bash shell相关命令记录(3)---echo、printf

1、echo命令

(1)显示转义字符

echo it's a test
结果为
it's a test

(2)显示变量

name=zhangsan
echo "${zhangsan} is a good boy"

(3)显示换行

echo -e "OK! 
"
echo "shoudao"
输出结果
OK!
shoudao

(4)显示重定向至文件

echo "IT IS A TEST" > test.txt
>为覆盖文件
>>为追加到文件末尾

2、printf

printf 命令用于格式化输出, 是echo命令的增强版。它是C语言printf()库函数的一个有限的变形,并且在语法上有些不同。

如同 echo 命令,printf 命令也可以输出简单的字符串:

$printf "Hello, Shell
"
Hello, Shell
$

printf 不像 echo 那样会自动换行,必须显式添加换行符( )。

format-string 为格式控制字符串,arguments 为参数列表。

这里仅说明与C语言printf()函数的不同:

  • printf 命令不用加括号
  • format-string 可以没有引号,但最好加上,单引号双引号均可。
  • 参数多于格式控制符(%)时,format-string 可以重用,可以将所有参数都转换。
  • arguments 使用空格分隔,不用逗号。

参考链接http://c.biancheng.net/cpp/view/1499.html

原文地址:https://www.cnblogs.com/chxmtl/p/12559539.html