我下面的所有环境都在在REDHAT LINUX9下试验的
在LINUX中,要使转义符生效,需加参数-e
从echo的变量开始说起
如:e c h o命令输出转义符以及变量。
-
# echo -e "\007your home is $HOME , you are connected on `tty`"
-
your home is /root , you are connected on /dev/pts/1
-
# echo -e "ayour home is $HOME , you are connected on `tty`"
-
your home is /root , you are connected on /dev/pts/1
-
#
复制代码
本例中
\007或a你可以让终端铃响一声
显示出$ H O M E目录,
并且可以让系统执行t t y命令(注意,该命令用键盘左上角的符号,法语中的抑音符引起来,不是单引号 )。
在e c h o命令输出之后附加换行,可以使用 n选项:
-
$ cat echod
-
#!/bin/sh
-
echo -e "this echo's 3 new linesnnn"
-
echo "OK"
复制代码
编辑一个新echod,如上内容,然后运行输出如下:
-
$ ./echod
-
this echo's 3 new lines
-
-
-
-
OK
-
$
复制代码
在e c h o语句中使用跳格符,记住别忘了加反斜杠:
-
$ echo -e "here is a tabthere are two tabsttok"
-
here is a tab here are two tabs ok
-
$
复制代码
把一个字符串输出到文件中,使用重定向符号>。
在下面的例子中一个字符串被重定向到一个名为m y f i l e的文件中:
-
$ echo "The log files have all been done"> myfile
复制代码
或者可以追加到一个文件的末尾,这意味着不覆盖原有的内容:
-
$ echo "$LOGNAME carried them out at `date`">>myfile
复制代码
现在让我们看一下m y f i l e文件中的内容:
The log files have all been done
sam carried them out at 六 11月 13 12:54:32 CST 2004
引号是一个特殊字符,所以必须要使用反斜杠来使s h e l l忽略它的特殊含义。
假设你希望使用e c h o命令输出这样的字符串:“/ d e v / r m t 0”,那么我们只要在引号前面加上反斜杠即可:
-
$ echo ""/dev/rmt0""
-
"/dev/rmt0"
-
$