39

I came across this expression in a bash script and it's really not easy to google for.

#$...
Mark Amery
  • 143,130
  • 81
  • 406
  • 459
CAN
  • 399
  • 1
  • 3
  • 3
  • 2
    http://superuser.com/questions/247127/what-is-and-in-linux – Corbin Mar 09 '12 at 07:08
  • 4
    If #$ is not a typo then AFAIK it is a comment line, if you meant $# then its the number of arguments passed to the script or the function – another.anon.coward Mar 09 '12 at 07:11
  • 1
    how about attaching the site where you found it? – franklin Mar 09 '12 at 19:16
  • 1
    Try to search for "dollar pound" or "pound dollar", that was how I did to find this page (first result of Google). – Wei Lin Aug 12 '15 at 21:21
  • You can also open the `bash` manpage and search for the patterns you meet but don't know the name of by typing `/` then the characters (don't forget to escape them). It may not be enough explanation, but at least you'll know their name, so it's easier to google them up. – jthulhu Jul 09 '22 at 13:59
  • About googling: This works nowadays: google for --> bash "$#" <-- actually, this was the top answer ;-) – Sybille Peters Mar 05 '23 at 07:45

5 Answers5

60

#$ does "nothing", as # is starting comment and everything behind it on the same line is ignored (with the notable exception of the "shebang").

$# is a variable containing the number of arguments passed to a shell script (like $* is a variable containing all arguments).

premek.v
  • 231
  • 4
  • 14
Christian.K
  • 47,778
  • 10
  • 99
  • 143
29

In bash this is generally a comment, everything after the hash (on the same line) is ignored. However, if your bash script is being passed to something unusual it could be interpreted by that.

For instance, if you submit a script to the Sun Grid Engine:

Any line beginning with hash-dollar, i.e., #$, is a special comment which is understood by SGE to specify something about how or where the job is run. In this case we specify the directory in which the job is to be run (#$ -cwd) and the queue which the job will join (#$ -q serial.q).

(source: http://talby.rcs.manchester.ac.uk/~rcs/_linux_and_hpc_lib/sge_intro.html)

Mark Amery
  • 143,130
  • 81
  • 406
  • 459
user1439517
  • 399
  • 3
  • 2
21

I just came across a #$ in a script and was wondering the same thing. There is answer which was not mentioned yet: In bash search and replace (see here).

Normally, it works like this:

${PARAMETER/PATTERN/STRING}

PATTERN can also include anchors, to match it either at the beginning or the end; namely # and %:

MYSTRING=ABCCBA
echo ${MYSTRING/#A/y}  # RESULT: yBCCBA
echo ${MYSTRING/%A/y}  # RESULT: ABCCBy

You can also use a variable for PATTERN - and skip the slashes to add to the confusion (and replace the pattern match with an empty string):

echo ${MYSTRING#$PATTERN}  # RESULT: BCCBA
echo ${MYSTRING%$PATTERN}  # RESULT: ABCCB

And here it is, a #$ in a bash string.

Florian Bw
  • 746
  • 1
  • 7
  • 16
10

It could be possible that the intended expression was $# instead of #$

$# Stores the number of command-line arguments that were passed to the shell program.

for example:

if [ $# -eq 0 ]; then
   echo "${USAGE}" >&2
   exit 1
fi

Displays the message stored in the $USAGE variable if the number of command-line arguments is 0.

eaykin
  • 3,713
  • 1
  • 37
  • 33
6

One place where you might see #$ come up is inside a arithmetic expression when changing base: http://www.gnu.org/software/bash/manual/bashref.html#Shell-Arithmetic

For example echo $((2#101)) will convert 101 to base two, and print 5.

I recently used this here:

calculate_time() {
    minutes=$((${1:0:1} + ${1:9:1}))
    seconds=$((${1:2:1} + ${1:11:1}))
    milliseconds=$((10#${1:4:3} + 10#${1:13:3}))
    result=$((${minutes}*60*1000 + ${seconds}*1000 + ${milliseconds}))
    echo $result
}

The first argument ($1) was in some format I don't remember that was taken using awk. The milliseconds argument was parsed as 009, for 9 milliseconds. However, bash treats numbers starting with a leading 0 as octal, so I converted them to decimal. It's not exactly using #$, but when I looked at this code after a while and tried to remember what was going on, I thought that was an expression at first and found this question, so this might help someone else.

gsgx
  • 12,020
  • 25
  • 98
  • 149
  • Funny enough, even SO can't display this correctly. It also doesn't display correctly on github. It looks fine in Sublime Text though. – gsgx Aug 30 '13 at 00:08
  • I've found [this function](https://github.com/ohmyzsh/ohmyzsh/blob/caff704f41d7a8cd01aa54b1202d6e3123e33fe6/lib/functions.zsh#L135-L189) in the oh my zsh repo, and I can't understand what the following syntax is doing `ord=$(( [##16] #byte ))`. I think it is similar to the above, but wanted to know exactly what was happening. I'm guessing it's base16 conversion? – kapad Nov 05 '20 at 03:51