如果输出的文本之中包含括号,则需要使用双引号。
$ echo "two words.txt"双引号还会对美元符号、斜杠和反引号转义。
$ echo "$USER $((2+2)) $(cal)"双引号还可以保存格式。
# 输出一行显示
$ echo $(cal)
# 输出分行显示
$ echo "$(cal)"双引号之中,反斜线可以用于转义,使得美元符号、斜杠和反引号不再会转义。
$ echo "The balance for user $USER is: \$5.00"
The balance for user me is: $5.00文件名之中的特殊符号,也可以用反斜线转义。
$ mv bad\&filename good_filename如果需要对反斜线本身转义,则需要连续使用两个反斜线(//)。
反斜线可以用来表示特殊符号。
\a响铃\b退格\n换行\r回车\t制表符
echo命令的-e参数,可以解释双引号之中的这些特殊符号。
$ echo -e "Time's up\a"单引号除了不会对美元符号、斜杠和反引号转义,其他与双引号一致。
$ echo text ~/*.txt {a,b} $(echo foo) $((2+2)) $USER
text /home/me/ls-output.txt a b foo 4 me
$ echo "text ~/*.txt {a,b} $(echo foo) $((2+2)) $USER"
text ~/*.txt {a,b} foo 4 me
$ echo 'text ~/*.txt {a,b} $(echo foo) $((2+2)) $USER'
text ~/*.txt {a,b} $(echo foo) $((2+2)) $USER重定向指的是将命令行输出写入指定位置。
>用来将标准输出重定向到指定文件。
$ ls -l /usr/bin > ls-output.txt如果重定向后的指定文件已经存在,就会被覆盖,不会有任何提示。
如果命令没有任何输出,那么重定向之后,得到的是一个长度为0的文件。因此,>具有创建新文件或改写现存文件、将其改为长度0的作用。
$ > ls-output.txt>>用来将标准输出重定向追加到指定文件。
$ ls -l /usr/bin >> ls-output.txt2>用来将标准错误重定向到指定文件。
$ ls -l /bin/usr 2> ls-error.txt标准输出和标准错误,可以重定向到同一个文件。
$ ls -l /bin/usr > ls-output.txt 2>&1
# 或者
$ ls -l /bin/usr &> ls-output.txt
# 追加到同一个文件
$ ls -l /bin/usr &>> ls-output.txt如果不希望输出错误信息,可以将它重定向到一个特殊文件/dev/null。
$ ls -l /bin/usr 2> /dev/null|用于将一个命令的标准输出,重定向到另一个命令的标准输入。
$ ls -l /usr/bin | less不要将>与|混淆。
$ ls > less上面命令会在当前目录,生成一个名为less的文本文件。
下面是标准错误重定向的一个例子。
invalid_input () {
echo "Invalid input '$REPLY'" >&2
exit 1
}
read -p "Enter a single item > "
[[ -z $REPLY ]] && invalid_inputtee命令用于同时将标准输出重定向到文件,以及另一个命令的标准输入。
$ ls /usr/bin | tee ls.txt | grep zip命令替换(command substitution)指的是将一个命令的输出,替换进入另一个命令。$(command)表示命令替换,另一种写法是使用反引号。
$ echo $(ls)
# 或者
$ echo `ls`
$ ls -l $(which cp)
# 或者
$ ls -l `which cp`basename命令清除 一个路径名的开头部分,只留下一个文件的基本名称。
#!/bin/bash
# file_info: simple file information program
PROGNAME=$(basename $0)
if [[ -e $1 ]]; then
echo -e "\nFile Type:"
file $1
echo -e "\nFile Status:"
stat $1
else
echo "$PROGNAME: usage: $PROGNAME file" >&2
exit 1
fi