zoukankan      html  css  js  c++  java
  • 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! 
    " # -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 自动添加换行符,我们可以手动添加 。

    #下面参数格式中的:d%(数值) s%(字符串) f%(小数)
    
    # 1、参数格式为双引号
    printf  "%s
    "  "1、参数格式为双引号:"
    printf "%d %s
    "  1 "abc"
    
    # 2、单引号与双引号显示效果一样 
    printf  "%s
    "  "2、单引号与双引号显示效果一样 :"
    printf '%d %s
    ' 1 "abc" 
    
    # 3、没有引号也可以输出
    printf  "%s
    "  "3、没有引号也可以输出:"
    printf %s abcdef
    printf  "%s
    "  #这行是为了打印一个空行
    
    # 4、格式只指定了一个参数,但多出的参数仍然会按照该格式输出,参数格式被重用
    printf  "%s
    "  "4、格式只指定了一个参数,但多出的参数仍然会按照该格式输出,参数格式被重用:"
    
    printf %s abc def
    
    printf "%s
    " abc def
    
    printf "%s %s %s
    " a b c d e f g h i j
    
    # 5、如果没有参数列表,那么 %s 用NULL代替,%d 用 0 代替
    printf  "%s
    "  "5、如果没有 参数列表:"
    printf "%s and %d 
    " 

    输出结果:

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

  • 相关阅读:
    判断闰年
    正向代理(Forward Proxy)与反向代理(Reverse Proxy)
    What do we need to know about Vue.js?(译)
    How To Create A GitHub Profile README(译)
    Building a carousel component in React using Hooks(译)
    What is Redux JS(译)
    Weekly Contest 197
    koa-compose源码阅读与学习
    js 事件循环消息队列和微任务宏任务
    记一次字节跳动的面试
  • 原文地址:https://www.cnblogs.com/riches/p/11940369.html
Copyright © 2011-2022 走看看