Shell 脚本学习笔记七:echo与printf命令

echo 命令:用于字符串的输出

 

1、显示普通字符串

     echo "It is a test";

    echo It is a test;

 

2、显示转义字符

     echo "\"It is a test\"";

 

3、显示变量

    echo "$name It is a test";

 

4、显示换行

     echo -e "OK! \n";       // -e:开启转义

    echo "It is a test";

 

5、显示不换行

     echo -e "OK! \c";       // -e:开启转义  \c:不换行

    echo "It is a test";

 

6、显示结果定向至文件

     echo "It is a test" > myfile

 

7、显示原输出字符串,不进行转义或取变量(用单引号)

    echo '$name\"'

 

8、显示命令执行结果

     echo `date`

 

printf 命令

printf 命令的语法:

     printf format-string [arguments...]

     参数:

        format-string:格式控制字符串

        arguments:参数列表

 

printf 格式只指定了一个参数,但多出的参数仍然会按照该格式输出,format-string 被重用,如果没有 arguments,那么 %s 用NULL代替,%d 用 0 代替

posted @ 2017-06-23 10:51  小小聪明屋  阅读(203)  评论(0编辑  收藏  举报