美文网首页
Shell 的打印

Shell 的打印

作者: tafanfly | 来源:发表于2019-07-30 16:29 被阅读0次

    echo

    可以使用echo对字符串进行打印,根据需求善用双引号和单引号。
    注意:echo 输出是自带换行的

     #!/bin/bash
    
    name='LiLei'
    echo "Your name is LiLei"
    echo "Your name is $name"
    echo "\"Your name is $name\""
    echo 'Your name is LiLei'
    echo 'Your name is $name'
    
    #sh test.sh
    Your name is LiLei
    Your name is LiLei
    "Your name is LiLei"
    Your name is LiLei
    Your name is $name
    
    • 开启转义 -e\n表示换行,\c表示不换行。
     #!/bin/bash
    
    echo "Your name is LiLei\n"
    echo -e "Your name is LiLei\n"
    echo "Your name is LiLei\c"
    echo -e "Your name is LiLei\c"
    
    #sh test.sh
    Your name is LiLei\n
    Your name is LiLei
    
    Your name is LiLei\c
    Your name is LiLei$ 
    
    • 结果重定向, 符号>
     #!/bin/bash
    
    echo "Your name is LiLei\n" >name.txt
    
    # result
    $ cat name.txt
    Your name is LiLei\n
    
    • 打印执行命令结果, 注意特殊符号 `
     #!/bin/bash
    
    echo `date`
    
    #sh test.sh
    Mon Jul 15 10:42:41 CST 2019
    

    printf

    可以使用printf对字符串进行格式化打印。
    注意:printf打印不会自动换行, 换行必须显式添加换行符(\n)

    相关文章

      网友评论

          本文标题:Shell 的打印

          本文链接:https://www.haomeiwen.com/subject/gaygkctx.html