有關命令擴展(Expansion)
echo:
[root@centos7rzc ~]# 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 [root@centos7rzc ~]# echo "*" #print * *
[root@centos7rzc ~]# echo D* #顯示以D開頭的文件或目錄 Desktop Documents Downloads [root@centos7rzc ~]# echo *s #顯示以s結尾的文件或目錄 Documents Downloads Pictures Templates Videos [root@centos7rzc ~]# echo [[:upper:]]* #顯示以大寫字母開頭的文件或目錄 Desktop Documents Downloads Music Pictures Public Templates Videos
[root@centos7rzc ~]# echo ~ #~擴展 /root
[root@centos7rzc ~]# echo $((3+2)) #算數擴展 5 [root@centos7rzc ~]# echo $(((3+2)*5)) 25 [root@centos7rzc ~]#
[root@centos7rzc ~]# echo $((3**2)) #**取冪 9 [root@centos7rzc ~]# echo $((3**4)) 81
大括號擴展
[root@centos7rzc ~]# echo A{1,2,3} A1 A2 A3
[root@centos7rzc testdir]# mkdir {2014..2015}-{01..05} [root@centos7rzc testdir]# ls 2014-01 2014-02 2014-03 2014-04 2014-05 2015-01 2015-02 2015-03 2015-04 2015-05
變量擴展
[root@centos7rzc testdir]# echo $HOSTNAME centos7rzc.jacen
命令替換
[root@centos7rzc testdir]# echo $(ls) 2014-01 2014-02 2014-03 2014-04 2014-05 2015-01 2015-02 2015-03 2015-04 2015-05 [root@centos7rzc testdir]# echo `ls` 2014-01 2014-02 2014-03 2014-04 2014-05 2015-01 2015-02 2015-03 2015-04 2015-05
[root@centos7rzc testdir]# ls -l $(which file) -rwxr-xr-x. 1 root root 19752 Jun 9 2014 /usr/bin/file
雙引號
[root@centos7rzc testdir]# echo this is a word! #多個空格被識別爲1個 this is a word! [root@centos7rzc testdir]# echo "this is a word! " #多個空格解決方案 this is a word!
[root@centos7rzc 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 [root@centos7rzc 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
單引號
[root@centos7rzc testdir]# echo $USER root [root@centos7rzc testdir]# echo '$USER' #單引號內不進行變量擴展 $USER
轉移字符
[root@centos7rzc testdir]# echo \$ \\ \t \n $ \ t n [root@centos7rzc testdir]# echo -e "\$ \\ \t \n" $ \ [root@centos7rzc testdir]# [root@centos7rzc testdir]# echo "\$ \\ "$'\t \n'#使用$可以起到-e的作用 $ \ [root@centos7rzc 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