环境 (shell) 变量

环境

  • shell 类型 :站在用户登陆的角度来说

  • 登陆式 shell

  • ssh

  • su - USER

  • 非登陆式 shell

  • GUI 的方式打开了一个 CLI shell

  • su USER

  • 脚本,在系统上自动执行

系统中的环境变量相关的文件有 5 种

profile 类 的文件:

设定环境变量

运行命令或脚本

/etc/profile

/etc/profile.d/*.sh

bashrc 类 的文件:

设定本地变量

定义命令别名

/etc/bashrc

~/.bash_profile

~/.bashrc

登录式 shell 如何读取配置文件?

/etc/profile ---> /etc/profile.d/*.sh ---> ~/.bash_profile ---> ~/.bashrc ---> /etc/bashrc

非登录式 shell 如何配置文件?

~/.bashrc ---> /etc/bashrc ---> /etc/profile.d/*.sh

变量

本地变量(局部变量)

  • 定义的变量只能作用于当前的 bash

环境变量

  • 五个文件都属于环境变的配置文件或脚本

  • export,子 bash 继承父 bash 通过 export 导出的环境变量

位置变量

  • 1 2 ....

特殊变量

$?(上一个命令执行状态的返回值)

  • 0:执行成功

  • 1-255:错误的执行

  • 1,2,127 预留错误

变量管理

撤销变量

  • unset $a unset a

列出当前系统的所有的环境变量

  • export

  • env

  • printenv