shell编程之echo printf 命令

shell中 echo 和printf 都能用作输出,printf可以算是echo的增强版

显示转义字符

echo \""abcdef\""

>>> "abcdef"

显示变量

age=23

echo "my age is $age"

>>>my name is 23文章地址https://www.yii666.com/article/756320.html

在使用的过程中,为了避免引起歧义,多使用${age}

显示换行

echo “ok\n“

echo "my name is liming"

>>>ok

>>>my name is liming

显示结果重定向

e cho "my name is limng "> file1

printf 格式化输出语句,但是后面必须加上回车换行符,不像echo 不用加

printf  “acbdef\n"文章来源地址:https://www.yii666.com/article/756320.html

>>>abcdef

>>>

# format-string为双引号文章来源地址https://www.yii666.com/article/756320.html

$ printf "%d %s\n" 1 "abc"

1 abc

# 单引号与双引号效果一样网址:yii666.com<

$ printf '%d %s\n' 1 "abc"

1 abc

# 没有引号也可以输出

$ printf %s abcdef

abcdef

# 格式只指定了一个参数,但多出的参数仍然会按照该格式输出,format-string 被重用

$ printf %s abc def

abcdef

$ printf "%s\n" abc def

abc

def网址:yii666.com

$ printf "%s %s %s\n" a b c d e f g h i j

a b c

d e f

g h i

j# 如果没有 arguments,那么 %s 用NULL代替,%d 用 0 代替

$ printf "%s and %d \n"

and 0

# 如果以 %d 的格式来显示字符串,那么会有警告,提示无效的数字,此时默认置为 0

$ printf "The first program always prints'%s,%d\n'" Hello Shell-bash: printf: Shell: invalid numbe

The first program always prints 'Hello,0'

$

版权声明:本文内容来源于网络,版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。文本页已经标记具体来源原文地址,请点击原文查看来源网址,站内文章以及资源内容站长不承诺其正确性,如侵犯了您的权益,请联系站长如有侵权请联系站长,将立刻删除

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信图片_20190322181744_03.jpg

微信扫一扫打赏

请作者喝杯咖啡吧~

支付宝扫一扫领取红包,优惠每天领

二维码1

zhifubaohongbao.png

二维码2

zhifubaohongbao2.png