【四月更文打卡】bash shell 中的特殊字符详解

136 阅读1分钟

bash shell 中的特殊字符详解


  • 井号常用作注释符号

1.注释示例

# This line is a comment.

2.某命令后注释,#号前需要添加一个空格

echo "A comment will follow." # Comment here.
#                            ^ Note whitespace before #

3.注释前亦可跟空白字符

   # A tab precedes this comment.

4.注释符号还可以被嵌入到带管道的命令当中

initial=( `cat "$startfile" | sed -e '/#/d' | tr -d '\n' |\
           sed -e 's/./. /g' -e 's/_/_ /g'` )
# Delete lines containing '#' comment character.
# 该命令用于删除包含#号的行

5.当然,在echo命令中被引用或者被转义的#号不会成为注释,#号也会出现在特定的参数替换结构中及一些数值常量表达式中

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#*:}       # 参数替换,不是注释
echo $(( 2#101011 ))  # 数制转换,不是注释

[root@centos7 /data/test]$echo ${PATH#*:} 
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@centos7 /data/test]$echo ${PATH}    
/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

6.标准的单双引用符号和转义符号("'/)都能转义#号

7.某些特定的模式匹配操作也使用#号


[semicolon] ; 分号

  • 分号一般用作命令分隔符,允许多个命令处于同一行

echo hello; echo there
if [ -x "$filename" ]; then    #  Note the space after the semicolon.
#+                   ^^
  echo "File $filename exists."; cp $filename $filename.bak
else   #                       ^^
  echo "File $filename not found."; touch $filename
fi; echo "File test complete."

[double semicolon] ;; 双分号

  • 双分号用作case语句中的语句结束符

  • bash4.0+的版本使用;;&或者;&作为结束符
case "$variable" in
  abc)  echo "$variable = abc" ;;
  xyz)  echo "$variable = xyz" ;;
esac