有关命令扩展(Expansion)
echo:
[[email protected] ~]# echo * #*被扩展成文件名 0227_2.sh 02273.sh 0227.sh anaconda-ks.cfg Desktop Documents Downloads err.txt initial-setup-ks.cfg Music perl5 Pictures Public Templates test test.txt Videos vmware-tools-distrib [[email protected] ~]# echo "*" #print * *
[[email protected] ~]# echo D* #显示以D开头的文件或目录 Desktop Documents Downloads [[email protected] ~]# echo *s #显示以s结尾的文件或目录 Documents Downloads Pictures Templates Videos [[email protected] ~]# echo [[:upper:]]* #显示以大写字母开头的文件或目录 Desktop Documents Downloads Music Pictures Public Templates Videos
[[email protected] ~]# echo ~ #~扩展 /root
[[email protected] ~]# echo $((3+2)) #算数扩展 5 [[email protected] ~]# echo $(((3+2)*5)) 25 [[email protected] ~]#
[[email protected] ~]# echo $((3**2)) #**取幂 9 [[email protected] ~]# echo $((3**4)) 81
大括号扩展
[[email protected] ~]# echo A{1,2,3} A1 A2 A3
[[email protected] testdir]# mkdir {2014..2015}-{01..05} [[email protected] testdir]# ls 2014-01 2014-02 2014-03 2014-04 2014-05 2015-01 2015-02 2015-03 2015-04 2015-05
变量扩展
[[email protected] testdir]# echo $HOSTNAME centos7rzc.jacen
命令替换
[[email protected] testdir]# echo $(ls) 2014-01 2014-02 2014-03 2014-04 2014-05 2015-01 2015-02 2015-03 2015-04 2015-05 [[email protected] testdir]# echo `ls` 2014-01 2014-02 2014-03 2014-04 2014-05 2015-01 2015-02 2015-03 2015-04 2015-05
[[email protected] testdir]# ls -l $(which file) -rwxr-xr-x. 1 root root 19752 Jun 9 2014 /usr/bin/file
双引号
[[email protected] testdir]# echo this is a word! #多个空格被识别为1个 this is a word! [[email protected] testdir]# echo "this is a word! " #多个空格解决方案 this is a word!
[[email protected] testdir]# echo "$(ls -l)" #请与下面的内容对比 total 40 drwxr-xr-x. 2 root root 4096 Mar 5 03:38 2014-01 drwxr-xr-x. 2 root root 4096 Mar 5 03:38 2014-02 drwxr-xr-x. 2 root root 4096 Mar 5 03:38 2014-03 drwxr-xr-x. 2 root root 4096 Mar 5 03:38 2014-04 drwxr-xr-x. 2 root root 4096 Mar 5 03:38 2014-05 drwxr-xr-x. 2 root root 4096 Mar 5 03:38 2015-01 drwxr-xr-x. 2 root root 4096 Mar 5 03:38 2015-02 drwxr-xr-x. 2 root root 4096 Mar 5 03:38 2015-03 drwxr-xr-x. 2 root root 4096 Mar 5 03:38 2015-04 drwxr-xr-x. 2 root root 4096 Mar 5 03:38 2015-05 [[email protected] testdir]# echo $(ls -l) #换行符被识别为分隔符 total 40 drwxr-xr-x. 2 root root 4096 Mar 5 03:38 2014-01 drwxr-xr-x. 2 root root 4096 Mar 5 03:38 2014-02 drwxr-xr-x. 2 root root 4096 Mar 5 03:38 2014-03 drwxr-xr-x. 2 root root 4096 Mar 5 03:38 2014-04 drwxr-xr-x. 2 root root 4096 Mar 5 03:38 2014-05 drwxr-xr-x. 2 root root 4096 Mar 5 03:38 2015-01 drwxr-xr-x. 2 root root 4096 Mar 5 03:38 2015-02 drwxr-xr-x. 2 root root 4096 Mar 5 03:38 2015-03 drwxr-xr-x. 2 root root 4096 Mar 5 03:38 2015-04 drwxr-xr-x. 2 root root 4096 Mar 5 03:38 2015-05
单引号
[[email protected] testdir]# echo $USER root [[email protected] testdir]# echo ‘$USER‘ #单引号内不进行变量扩展 $USER
转移字符
[[email protected] testdir]# echo \$ \\ \t \n $ \ t n [[email protected] testdir]# echo -e "\$ \\ \t \n" $ \ [[email protected] testdir]# [[email protected] testdir]# echo "\$ \\ "$‘\t \n‘#使用$可以起到-e的作用 $ \ [[email protected] testdir]#
\a Bell (“Alert” - causes the computer to beep)
\b Backspace
\n Newline. On Unix-like systems, this
produces a linefeed.
\r Carriage return
\t Tab
时间: 2024-10-22 11:49:46