zoukankan      html  css  js  c++  java
  • shell变量(二)

    变量名的命名规范:

    1.命名只能使用英文字母、数字和下划线,且不能以数字开头;

    2.不能存在空格‘;

    3.不能使用标点符号;

    4.不能使用bash里的关键字(可使用help命令查看保留关键字)

    变量的类型:局部变量、环境变量、shell变量(后续用到试再详细介绍)

    使用变量

    [root@ipha-dev71-1 exercise_shell]# cat variable.sh 
    #!/bin/sh
    your_name="wangmengmeng"     #注意等号左右不需要空格
    echo $your_name
    echo ${your_name}            # {}不是必须,但是推荐加上,这是个好的编程习惯
    [root@ipha-dev71-1 exercise_shell]# ./variable.sh 
    wangmengmeng
    wangmengmeng

    只读变量

    [root@ipha-dev71-1 exercise_shell]# cat read_only.sh 
    #!/bin/sh
    my_name="wmm"
    readonly my_name
    my_name="wangmm"
    [root@ipha-dev71-1 exercise_shell]# ./read_only.sh 
    ./read_only.sh: line 4: my_name: readonly variable

    删除变量

    [root@ipha-dev71-1 exercise_shell]# cat del_var.sh 
    #!/bin/sh
    my_name="wmm"
    echo ${my_name}
    unset my_name
    echo ${my_name}         # 第二次不会有输出结果
    [root@ipha-dev71-1 exercise_shell]# ./del_var.sh 
    wmm

    shell字符串:单引号、双引号均可

    单引号限制:单引号里的任何字符都会原样输出,单引号字符串中的变量是无效的;

                         单引号字串中不能出现单独一个的单引号(对单引号使用转义符后也不行),但可成对出现,作为字符串拼接使用。

    [root@ipha-dev71-1 exercise_shell]# cat simple_str.sh 
    #!/bin/sh
    str='this is a string'
    echo ${str}
    [root@ipha-dev71-1 exercise_shell]# ./simple_str.sh 
    this is a string

    双引号的优点:双引号里可以有变量;双引号里可以出现转义字符。

    [root@ipha-dev71-1 exercise_shell]# cat double_str.sh 
    #!/bin/sh
    my_name='wangmm'
    str="hello,I konw you are "${my_name}"!
    "
    echo -e $str
    [root@ipha-dev71-1 exercise_shell]# ./double_str.sh 
    hello,I konw you are "wangmm"!
  • 相关阅读:
    用任务计划管理计划任务对付任务计划-禁止WPS提示升级
    破解激活Win10无风险?激活后删除激活工具无影响===http://www.pconline.com.cn/win10/693/6932077_all.html#content_page_4
    VS2013 密钥 – 所有版本
    2017面试题1
    模拟锚点
    输入框被软键盘遮
    资源(GitHub)
    全国城市部分js
    subline3 插件
    app下载——js设备判断
  • 原文地址:https://www.cnblogs.com/wang-mengmeng/p/11418782.html
Copyright © 2011-2022 走看看