天天看点

shell echo 换行 不换行 打印换行

换行与不换行

 echo默认是有换行的, -n的时候, 是不换行的, 看下man的介绍:

[email protected]:~$ man echo | grep "\-n" -C2
       Echo the STRING(s) to standard output.

       -n     do not output the trailing newline

       -e     enable interpretation of backslash escapes
[email protected]:~$ 
           

看下实际例子:

[email protected]:~$ echo "abc" > a.txt
[email protected]:~$ xxd a.txt 
0000000: 6162 630a                                abc.
[email protected]:~$ 
[email protected]:~$ 
[email protected]:~$ 
[email protected]:~$ echo -n "abc" > a.txt
[email protected]:~$ xxd a.txt 
0000000: 6162 63                                  abc
[email protected]:~$ 
           

其中, 0a就是换行。

打印换行

echo -e 

其中 引号可能为 双引号 或 单引号 具体实际情况自行测试。centos7为单引号换行成功,双引号换行不成功。

[~]#echo -e "Hello world.\nHello sea"
Hello world.
Hello sea