What does #$ do in bash? (aka: Hash dollar sign, p

2020-05-24 19:52发布

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

#$...

Thanks for your help!

标签: bash shell
5条回答
我命由我不由天
2楼-- · 2020-05-24 20:10

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.

查看更多
女痞
3楼-- · 2020-05-24 20:12

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

$#, as you had it, prints the number of arguments passed to a shell script (like $* prints all arguments).

查看更多
成全新的幸福
4楼-- · 2020-05-24 20:15

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)

查看更多
迷人小祖宗
5楼-- · 2020-05-24 20:17

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.

查看更多
我命由我不由天
6楼-- · 2020-05-24 20:20

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.

查看更多
登录 后发表回答