Shell学习(五)Shell输出命令

一、echo命令

#1、直接显示字符串
echo "It is a test"
#输出:It is a test

#2、显示转义字符
echo "\"It is a test\""
#输出:"It is a test"

#3、显示变量
name=Shell
echo "$name It is a test"
#输出:Shell is a test

#4、显示换行
echo -e "OK! \n" # -e 开启转义
echo "It is a test"
#输出:
    #OK!
    #
    #It is a test

#5、显示不换行
echo -e "OK! \c" # -e 开启转义 \c 不换行
echo "It is a test"
#输出:OK! It is a test

#6、原样输出,不取变量和转译(用单引号echo '$name\"'
#输出:$name\"

#7、显示命令执行结果(用反引号echo `date`
#输出:Wed Nov 27 10:05:37 CST 2019

二、printf命令

  格式:printf 参数格式  参数列表

  PS:默认 printf 不会像 echo 自动添加换行符,我们可以手动添加 \n。

#下面参数格式中的:d%(数值) s%(字符串) f%(小数)

# 1、参数格式为双引号
printf  "%s\n"  "1、参数格式为双引号:"
printf "%d %s\n"  1 "abc"

# 2、单引号与双引号显示效果一样 
printf  "%s\n"  "2、单引号与双引号显示效果一样 :"
printf '%d %s\n' 1 "abc" 

# 3、没有引号也可以输出
printf  "%s\n"  "3、没有引号也可以输出:"
printf %s abcdef
printf  "%s\n"  #这行是为了打印一个空行

# 4、格式只指定了一个参数,但多出的参数仍然会按照该格式输出,参数格式被重用
printf  "%s\n"  "4、格式只指定了一个参数,但多出的参数仍然会按照该格式输出,参数格式被重用:"

printf %s abc def

printf "%s\n" abc def

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

# 5、如果没有参数列表,那么 %s 用NULL代替,%d 用 0 代替
printf  "%s\n"  "5、如果没有 参数列表:"
printf "%s and %d \n" 

输出结果:

详细讲解请参考:戳这里~

猜你喜欢

转载自www.cnblogs.com/riches/p/11940369.html