变量的赋值

  • 不区分类型,即Shell的变量是弱类型
  • 变量名的命名规则
    • 字母,数字,下划线
    • 不能以数字开头
  • 变量名=变量值(注:=左右不能有空格)
    • a=123
  • 使用let为变量赋值
    • let a=123
  • 还可以将命令赋值给变量
    • l=ls
  • 变量值有特殊字符比如空格,使用单引号或者双引号

    • str1=hello
    • str2=’hello world’
    • str3=”I’m good”

      变量的引用

  • ${变量名} -> 对变量引用

  • echo ${变量名} -> 查看变量的值
  • ${变量名}在部分情况下可以简写成$变量名

    • str1=”hello world”
    • echo $str1 此时可以省略
    • echo ${str1}23 此时不能省略

      变量的作用范围

  • 变量的默认作用范围是只对当前的shell或者终端生效,对父shell,子shell,兄弟shell都不生效

  • 变量的导出 -> 让子shell 能够访问到父shell里定义的变量
    • export 变量名=变量值
    • export 变量名
  • 变量的值取消 -> unset 变量名