shell中的特殊字符【# ; ;; . ” ’ , \ / ` : !】
# 井号
表示注释的开始,#!除外
可以嵌入到pipe中,如:
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
- 其他用法
echo The # here begins a comment.
echo ${PATH#*:} # Parameter substitution, not a comment.
echo $(( 2#101011 )) # Base conversion, not a comment.
- 一些正则表达式里会用到# todo 举例
; 分号
用于一行分割命令
需要用;本身字符的时候,需要转义 \;
;;双分号
- 用于case命令里的分隔符
case "$variable" in
abc) echo "\$variable = abc" ;;
xyz) echo "\$variable = xyz" ;;
esac
. 点号
- shell内置命令 类似于source ,执行一条shell命令
[shanjun@: shell]$ . test1.sh
Part 1 of script.
Part 2 of script.
[shanjun@: shell]$ source test1.sh
Part 1 of script.
Part 2 of script.
文件名的前缀,表示一个隐藏文件
文件目录,表示当前目录
字符匹配,匹配一个单一的字符
”双引号
- 部分引用。双引号包围的内容可以允许变量扩展,也允许转义字符的存在。如果字符串内出现双引号本身,需要转义,因此不一定双引号是成对的。
’ 单引号
- 单引号括住的内容,被视为单一字符串,引号内的禁止变量扩展,所有字符均作为字符本身处理(除单引号本身之外),单引号必须成对出现。
, 逗号
- 操作符 : 用在连接一连串的数学表达式中,这串数学表达式均被求值,但只有最后一个求值结果被返回。
#!/bin/bash
let t1=((a=5+1, b=7+2))
echo t1=$t1, a=$a, b=$b
## 这个$t1=$b;
- 操作符,用来连接字符串
for file in /{,usr/}bin/*calc
# ^ Find all executable files ending in "calc"
#+ in /bin and /usr/bin directories.
do
if [ -x "$file" ]
then
echo $file
fi
done
# /bin/ipcalc
# /usr/bin/kcalc
# /usr/bin/oidcalc
# /usr/bin/oocalc
# Thank you, Rory Winston, for pointing this out.
- 用于参数替代,表示首字母小写,如果是两个逗号,则表示全部小写
a="ATest"
echo ${a,}
echo ${a,,}
## 前面输出aTest,后面输出的是atest。
\ 转义字符
放在特殊符号之前,转义特殊符号的作用,仅表示特殊符号本身,这在字符串中常用;
放在一行指令的最末端,表示紧接着的回车无效(其实也就是转义了Enter),后继新行的输入仍然作为当前指令的一部分。
/ 斜线
- 作为路径的分隔符,路径中仅有一个斜杆表示根目录,以斜杆开头的路径表示从根目录开始的路径;
/home/bozo/projects/Makefile
- 在作为运算符的时候,表示除法符号。
a=4/2
` 反引号
- 命令替换。这个引号包围的为命令,可以执行包围的命令,并将执行的结果赋值给变量。如:
a=`dirname '/tmp/x.log'`
## 后面dirname返回的结果会赋值给a,
## 注意,此处Mitchell特地使用了反引号和单引号,注意区别。
: 冒号
-shell 内置命令 什么都不做 等价于true
:
echo $? # 0
- 用于死循环
while :
do
operation-1
operation-2
...
operation-n
done
# Same as:
# while true
# do
# ...
# done
- if/then 中的占位符
if condition
then : # Do nothing and branch ahead
else # Or else ...
take-some-action
fi
- 放在必须要有两元操作的地方作为分隔符 todo 例子
: ${username=`whoami`}
# ${username=`whoami`} Gives an error without the leading :
# unless "username" is a command or builtin...
: ${1?"Usage: $0 ARGUMENT"} # From "usage-message.sh example script.
- 用于here document中的命令
#!/bin/bash
: <<TESTVARIABLES
${HOSTNAME?}${USER?}${MAIL?} # Print error message if one of the variables not set.
TESTVARIABLES
exit $?
- 测试变量是否赋值
: ${HOSTNAME?} ${USER?} ${MAIL?}
# Prints error message
#+ if one or more of essential environmental variables not set.
- 在参数替换中为字符串变量赋值,在重定向操作(>)中,把一个文件长度截断为0(:>>这样用的时候,目标存在则什么都不做),这个只能在普通文件中使用,不能在管道,符号链接和其他特殊文件中使用;
: > data.xxx # File "data.xxx" now empty.
# Same effect as cat /dev/null >data.xxx
# However, this does not fork a new process, since ":" is a builtin.
- 甚至你可以用来注释(#后的内容不会被检查,但:后的内容会被检查,如果有语句如果出现语法错误,则会报错) —不推荐这种写法;
: This is a comment that generates an error, ( if [ $x -eq 3] ).
- 你也可以作为域分隔符,比如环境变量$PATH中,或者passwd中,都有冒号的作为域分隔符的存在;
bash$ echo $PATH
/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/sbin:/usr/sbin:/usr/games
- 你也可以将冒号作为函数名,不过这个会将冒号的本来意义转变(如果你不小心作为函数名,你可以使用unset -f : 来取消function的定义)
:()
{
echo "The name of this function is "$FUNCNAME" "
# Why use a colon as a function name?
# It's a way of obfuscating your code.
}
:
# The name of this function is :
- 可以用作在空函数里的占位
not_empty ()
{
:
} # Contains a : (null command), and so is not empty.
!感叹号
- 取反一个测试结果或退出状态
true # The "true" builtin.
echo "exit status of \"true\" = $?" # 0
! true
echo "exit status of \"! true\" = $?" # 1
# Note that the "!" needs a space between it and the command.
# !true leads to a "command not found" error
#
# The '!' operator prefixing a command invokes the Bash history mechanism.
true
!true
# No error this time, but no negation either.
# It just repeats the previous command (true).
# =========================================================== #
# Preceding a _pipe_ with ! inverts the exit status returned.
ls | bogus_command # bash: bogus_command: command not found
echo $? # 127
! ls | bogus_command # bash: bogus_command: command not found
echo $? # 0
# Note that the ! does not change the execution of the pipe.
# Only the exit status changes.
# =========================================================== #
# Thanks, Stéphane Chazelas and Kristopher Newsome.
在命令行中,可以用于历史命令机制的调用,你可以试试!$,!#,或者!-3看看,不过要注意,这点特性不能在脚本文件里面使用(被禁用)。例子如上
在不同的环境里面,感叹号也可以出现在间接变量引用里面;
更多神奇的用法参见 Linux命令行下”!”的十个神奇用法