#$ 在 bash 中做什么?(又名:哈希美元符号,英镑美元符号)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9630203/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does #$ do in bash? (aka: Hash dollar sign, pound dollar sign)
提问by CAN
I came across this expression in a bash script and it's really not easy to google for.
我在 bash 脚本中遇到了这个表达式,谷歌搜索真的不容易。
#$...
Thanks for your help!
谢谢你的帮助!
回答by Christian.K
#$
does "nothing", as #
is starting comment and everything behind it on the same line is ignored (with the notable exception of the "shebang").
#$
不做任何事情,就像#
开始评论一样,并且在同一行后面的所有内容都被忽略(“shebang”除外)。
$#
, as you had it, prints the number of arguments passed to a shell script (like $*
prints all arguments).
$#
,正如您所拥有的,打印传递给 shell 脚本的参数数量(如$*
打印所有参数)。
回答by user1439517
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.
在 bash 中,这通常是一个注释,哈希之后的所有内容(在同一行)都将被忽略。但是,如果您的 bash 脚本被传递给一些不寻常的东西,它可能会被解释。
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)
例如,如果您向 Sun Grid Engine 提交一个脚本:“任何以 hash-dollar 开头的行,即 #$,都是 SGE 理解的特殊注释,用于指定有关如何或在何处运行作业的信息。在在这种情况下,我们指定要运行作业的目录 (#$ -cwd) 和作业将加入的队列 (#$ -q serial.q)。” (来源:http: //talby.rcs.manchester.ac.uk/~rcs/_linux_and_hpc_lib/sge_intro.html)
回答by Florian Bw
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).
我刚刚#$
在脚本中遇到了一个并且想知道同样的事情。有一个尚未提及的答案:在 bash 搜索和替换中(参见此处)。
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 %
:
PATTERN
还可以包含anchors,以在开头或结尾匹配它;即#
和%
:
MYSTRING=ABCCBA
echo ${MYSTRING/#A/y} # RESULT: yBCCBA
echo ${MYSTRING/%A/y} # RESULT: ABCCBy
You can also use a variable for PATTERN
- andskip the slashes to add to the confusion (and replace the pattern match with an empty string):
您还可以使用变量PATTERN
-并跳过斜线以增加混淆(并将模式匹配替换为空字符串):
echo ${MYSTRING#$PATTERN} # RESULT: BCCBA
echo ${MYSTRING%$PATTERN} # RESULT: ABCCB
And here it is, a #$
in a bash string.
在这里,#$
bash 字符串中的 a 。
回答by eaykin
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.
$#存储传递给 shell 程序的命令行参数的数量。
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.
如果命令行参数的数量为 0,则显示 $USAGE 变量中存储的消息。
回答by gsingh2011
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
您可能会看到的一个地方#$
是更改基数时的算术表达式内: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
.
例如echo $((2#101))
将转换101
为基数二,并打印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.
第一个参数 ( $1
) 的格式我不记得是使用awk
. 毫秒参数被解析为009
, 9 毫秒。但是,bash 将以 0 开头的数字视为八进制,因此我将它们转换为十进制。它并不完全是 using #$
,但是当我在一段时间后查看此代码并试图记住发生了什么时,我一开始以为这是一个表达式并发现了这个问题,因此这可能对其他人有所帮助。