基本的bash shell命令--3

shell中的环境变量成为全局变量和局部变量。全局变量采用全大写字母。

1.printenv

  查看系统的全局变量。

  显示单个环境变量的值采用echo命令。

  eg:echo $PATH  显示PATH环境变量的内容。

2.set

  显示为某个特定进程设置的所有环境变量,包括全局变量。

3.设置局部环境变量

  在该shell环境下定义的局部变量不能在其子shell中使用。

  eg:  test=testing     如果要给变量赋一个含有空格的字符串的值,应该采用单引号。(test=‘testing a  word')

       echo $test        定义了test变量然后输出

4.设置全局环境变量

  先定义局部变量,然后使用export将局部环境变量导出到全局环境中。该全局变量在设定该全局环境变量的进程创建的所有子进程中都是可见的。

  eg:  test=’testing a  word'

       export test

       bash 

      echo $test   输出testing a word

5.unset

  删除一个已经存在的环境变量。

  eg:unset test

6.可变数组

  环境变量很好的一个功能时可以作为数组使用。

  eg:test={one two three  four  five}

     echo $test  输出one

     echo ${test[2]}  输出three

     echo ${test[*]}  输出one two three  four  five

       test[2]=six  修改数组的第三个数据

     echo ${test[*]}  输出one two six four  five

     unset test[2]  删除数组第三个数据

     echo ${test[*]}  输出one two four  five

     echo ${test[2]}  输出为空

7.alias

  为通用命令(和他们的参数一起)创建一个别名。该别名只能在定义它们的shell进程中有效。

  eg:  alias  li='ls  -il'

posted @ 2016-11-19 19:48  胖子到瘦子  阅读(298)  评论(0编辑  收藏  举报