《Shell Programming in Unix, Linux and OS X, 4th Edition 》 笔记之Can I Quote You On That?

来源:互联网 发布:php发送短信 编辑:程序博客网 时间:2024/06/06 03:13

命令替换

There are two ways in the shell to perform command substitution: by enclosing the command in back quotes or surrounding it with the (...)construct.shell(…) 实现。

wdy@wdy:~$ echo The date and time is 'date'The date and time is datewdy@wdy:~$ echo The date and time is "date"The date and time is datewdy@wdy:~$ echo The date and time is `date`The date and time is 20170118日 星期三 15:24:25 CSTwdy@wdy:~$ echo The date and time is $(date)The date and time is 20170118日 星期三 15:35:59 CST

$(…)允许嵌套以及管道命令。

wdy@wdy:~$ echo "you have $(who | wc -l) peopele"you have 17 peopele

当filelist被shell替换的时候,里面的回车符号被去掉了

$ filelist=$(ls)$ echo $filelistaddresses intro lotsaspaces names nu numbers phonebook stat$ echo "$filelist"addressesintrolotsaspacesnamesnunumbersphonebookstat$
$ name="Ralph Kramden"$ name=$(echo $name | tr '[a-z]' '[A-Z]')$ echo $nameRALPH KRAMDEN$
$ file=exec.o$ lastchar=$(echo $file | sed 's/.*\(.\)$/\1/')$ echo $lastcharo$
$ filename=/users/steve/memos$ filename=$(echo $filename | tr "$(echo $filename | cut -c1)" "^")$ echo $filename^users^steve^memos$
0 0