天天看點

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