您的位置:首页 > 运维架构 > Shell

Bash Shell 中的特殊字符

2015-01-21 15:02 134 查看
什么是Bash Shell中的特殊字符?某些特殊字符在Bash Shell里不是表示其字符含义本身,而是另有含义,称为:meta-meaning。
1.字符:#

代表注释,如果有一行以#开始,那么意味着这行不会被执行,只是一行说明注释。
例如:
# this is comments
这是一行注释。

但是这里有个例外,那就是脚本的第一行我们经常看到的:
#!/bin/bash
这个指示一个脚本的解释器,说明这个脚本是由/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.
这个惨了,被截断了,只输出: The
echo ${PATH#*:}
这个是参数替换,不是注释符。
echo $(( 2#101011 ))
这个是基数指示符,也不是注释符,说明后面的数是二进制的。

2.字符:;

分号[semicolon],作为命令的分割符,说明一条命令到此结束。使用分号,你可以在一行写多条命令,这个我们经常见。
例如:
hdfeel$ echo hello; echo world;
hello
world
hdfeel$


3.字符:;;
两个分号在一起[double semicolon],这个只用在case语句里:
case "$variable" in
abc)  echo "\$variable = abc" ;;
xyz)  echo "\$variable = xyz" ;;
esac


4.字符:"

双引号[double quote]表示部分引用[partial quoting],多数情况下,双引号内的字符原样输出,但其中的变量会被替换。比如:
hdfeel$ fileName="a.txt"
hdfeel$ echo "the file name is $fileName"
hdfeel$ the file name is a.txt
这可能正是我们需要的。如果你想原样输出,那么用单引号,或者使用反斜杠\对$进行转义。
echo "the file name is \$fileName"
echo 'the file name is $fileName'


5.字符:’

单引号[single quote],代表全引用[full quoting],引号内的字符全部原样输出,即使有变量也不做相应的替换。
例子上面有了,不再重复了。

6.字符:,

逗号[comma operator],连接2个算式,但是只有最后一个算式的值被返回。
hdfeel$ let "t2 = ((a = 9, 15 / 3))"


上面的语句把a赋值为9,并把15除以3的结果5,赋值给t2.
另外,逗号操作符,也可以连接字符串:
for file in /{,usr/}bin/*calc
#             ^    在/bin和/usr/bin目录里,找出所有以"calc"结尾的可执行文件
#+
do
if [ -x "$file" ]
then
echo $file
fi
done
# /bin/ipcalc
# /usr/bin/kcalc
# /usr/bin/oidcalc
# /usr/bin/oocalc


7.字符:

反斜杠[backslash]就是换码符[escape],为单个字符的引用机制,通过对字符的引用来改变它本来的含义。
比如:
hdfeel$ echo "\"";   // 输出双引号
hdfeel$ "
hdfeel$ x=abc
hdfeel$ echo “$x”   // 输出变量的值
hdfeel$ abc
hdfeel$ echo “\$x”  // 原样输出
hdfeel$ $x


8.字符:`

命令替换符,就是键盘的左上角波浪符下面的字符,不要看错了,不是单引号噢,<code>`</code>中间放一个可执行的命令<code>`</code>,这个符号也叫反引号。
举个小例子:
#! /bin/bash
inFile=“example.txt"
maxLen=12
#
while read line; do
len=`echo $line | awk '{print length($0)}'`    //  子命令被放到了反引号里面执行。
if [ $len -lt $maxLen ]; then
echo $line
fi
done < $inFile
上面的程序打印出了少于12个字符的行。

9.冒号:

冒号[colon],称为空命令[null command],即什么都不执行,返回值为true,也就是0,属于Bash的内置命令。
下面2段代码把冒号当成了判断条件:
while :
do
operation-1
operation-2
...
operation-n
done
和上面的代码实现一样的功能
while true
do
operation-1
operation-2
...
operation-n
done
有时它也被用来当占位符:
if condition
then :   # Do nothing and branch ahead
else     # Or else ...
take-some-action
fi


冒号:也用来当作域分隔符,你在/etc/passwd和$PATH变量中可以看到它:
shells hdfeel$ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
shells hdfeel$


最糟心的时,冒号这东西还可以做函数名,如果想让代码把人搞晕的话,这么搞
:()
{
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.


下面的这个比较实用:
{a..z} 生成字符a到z之间的所有字符,当然也可以是数字,比如:{1..9}
体会下这个代码
echo {a..z} # a b c d e f g h i j k l m n o p q r s t u v w x y z
# Echoes characters between a and z.
echo {0..3} # 0 1 2 3
# Echoes characters between 0 and 3.
base64_charset=( {A..Z} {a..z} {0..9} + / = )
# Initializing an array, using extended brace expansion.
# From vladz's "base64.sh" example script.


上面解释了一些Bash Shell中的部分特殊字符,在<a Advanced Bash-Scripting Guide中有更多、更详细的介绍,有兴趣可以参考。

原文:http://www.hdfeel.net/2015/01/bash-shell-special-character/

本文出自 “HDFeel的IT视点” 博客,请务必保留此出处http://hdfeel.blog.51cto.com/9805995/1606620
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: