• 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"!
  • 相关阅读:
    SpringBoot集成Mybatis
    SpringBoot环境搭建
    阻止a标签的默认行为有哪几种方法
    mouseover 和mouseenter的区别;冒泡与捕获的区别;冒泡与捕获的如何阻止
    操作dom
    谷歌火狐,IE8以及其他浏览器获取页面滚动出去的距离以及封装
    return的返回值
    js进阶之js三大家族:offset,scroll,client
    固定导航栏,获取页面可视区域的大小,响应式布局,事件对象极其三大坐标系
    document.write,innerHTML,createElement三者的区别
  • 原文地址:https://www.cnblogs.com/wang-mengmeng/p/11418782.html
Copyright © 2020-2023  润新知