shell中的特殊字符
- # 注釋字符
- ; 行分割字符
- ;; case 終結(jié)字符
- . source 命令
- :空命令
注釋
以 # 開頭的行是一行注釋。
#this is a comment
#號可以在本行的空白或者tab后面
#this is also a comment
注釋也可以嵌入在管道中:
initial=( `cat "$startfile" | sed -e '/#/d' | tr -d '\n' |\
# Delete lines containing '#' comment character.
sed -e 's/\./\. /g' -e 's/_/_ /g'` )
# Excerpted from life.sh script
注意:有些shell命令是在一行中的,對于這種命令,shell注釋并沒有提供注釋段的完結(jié)表示符。因此后面的內(nèi)容都會被注釋掉,如果想繼續(xù)寫命令,需要另起一行。
對于下面的情況:
#!/bin/bash
echo "The # here does not begin a comment."
echo 'The # here does not begin a comment.'
echo The \# here does not begin a comment.
echo The # here begins a comment.
echo ${PATH#*:} # Parameter substitution, not a comment.
echo $(( 2#101011 )) # Base conversion, not a comment.
- 在echo后面被引號引起來的,或者轉(zhuǎn)義之后的不是注釋。
- 參數(shù)替換,也不是注釋
- 進制轉(zhuǎn)換,也不是注釋
關(guān)于case
#!/bin/bash
variable=$1
case "$variable" in
abc) echo "\$variable = abc";;
xyz) echo "\$variable = xyz";;
esac
空命令
等價于Nop, 什么都不做。運行之后,它的返回值為0,也就是shell的true。
;
echo $? #0
或者在一個死循環(huán)中:
while :
do
operation-1
operation-2
...
operation-n
done
# Same as:
# while true
# do
# ...
# done
在if 中占用空的分支,在shell中,每個分支必須要有動作。
if condition
then : # Do nothing and branch ahead
else # Or else ...
take-some-action
fi
另外可以用作命令占位符,例如如下的場景。
需要將當前用戶名通過反引號賦值給username 。
: ${username=`whoami`}
# ${username=`whoami`} Gives an error without the leading :
# unless "username" is a command or builtin...
#!/bin/bash
: ${username=`whoami`}
echo $username
阻塞,防止產(chǎn)生新進程。
: >data