Linux shell 环境变量 All In One

发布时间 2023-09-08 11:55:26作者: xgqfrms

Linux shell 环境变量 All In One

  1. 全局环境变量,system wide environment 系统级环境变量
  2. 局部环境变量,local wide environment 用户自定义环境变量

shell 的 3 种启动方式

  1. 登录默认的交互式 shell
  2. 非登录交互式 shell
  3. 运行脚本非交互式 shell

shell 的常见类型

  • sh
  • bash
  • csh
  • zsh
# 查看 shell 路径
$ which sh
/bin/sh

$ which bash
/bin/bash

$ which csh
/bin/csh

$ which zsh
/bin/zsh

全局环境变量

# 查看全局环境变量
$ env
# OR
$ printenv
__CFBundleIdentifier=com.apple.Terminal
TMPDIR=/var/folders/by/24_sdryj5sv4qvxcyjfnn64c0000gn/T/
XPC_FLAGS=0x0
TERM=xterm-256color
SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.NNA6aLv9vF/Listeners
SECURITYSESSIONID=186a6
XPC_SERVICE_NAME=0
TERM_PROGRAM=Apple_Terminal
TERM_PROGRAM_VERSION=447
SHELL=/bin/zsh
HOME=/Users/xgqfrms-mm
LOGNAME=xgqfrms-mm
USER=xgqfrms-mm
PATH=/bin:/usr/bin:/usr/local/bin:/Users/xgqfrms-mm/.nvm/versions/node/v18.12.0/bin:/usr/local/opt/mongodb-community@4.2/bin:/Users/xgqfrms-mm/.nvm/versions/node/v18.12.0/bin:/bin:/usr/bin:/usr/local/bin:/Users/xgqfrms-mm/.nvm/versions/node/v18.12.0/bin:/Library/Frameworks/Python.framework/Versions/3.11/bin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/Apple/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/usr/local/mysql/bin/
SHLVL=1
PWD=/Users/xgqfrms-mm
OLDPWD=/Users/xgqfrms-mm
ZSH=/Users/xgqfrms-mm/.oh-my-zsh
PAGER=less
LESS=-R
LSCOLORS=Gxfxcxdxbxegedabagacad
NVM_DIR=/Users/xgqfrms-mm/.nvm
NVM_CD_FLAGS=-q
NVM_BIN=/Users/xgqfrms-mm/.nvm/versions/node/v18.12.0/bin
LC_CTYPE=UTF-8
_=/usr/bin/env

设置系统环境变量的方式

  1. 通过命令行
  2. 通过 shell script 脚本文件
  3. 通过 shell 启动配置文件,如 /etc/profile
# 设置系统环境变量, 全大写字母
# key=value 等号两边不可以有空格⚠️
$ export GITHUB_ENV_USERNAME=xgqfrms

$ echo $GITHUB_ENV_USERNAME

父 shell 设置的系统环境变量,子 shell 可以访问该系统环境变量;
⚠️ 但是子 shell如果修改删除该系统环境变量只会对子 shell 自己有效,不会影响到父 shell,即不会向上传播变化。


局部环境变量

# 查看当前用户拥有的所有环境变量 (局部环境变量 + 系统环境变量)
$ set
# ⚠️  没有单独查看当前局部环境变量的命令


# 设置局部环境变量, 全小写字母
# key=value 等号两边不可以有空格⚠️
$ github_env_username=xgqfrms

$ echo $github_env_username

父 shell 设置的局部环境变量,子 shell 不可以访问该局部环境变量

# 父 shell
$ github_env_username=xgqfrms
$ echo $github_env_username
xgqfrms

$ set | grep github_env_username
github_env_username=xgqfrms
# 子 shell
$ bash
$ echo $github_env_username

image

demos

系统环境变量

$ echo $HOME
/Users/xgqfrms-mm
$ echo $PATH
/bin:/usr/bin:/usr/local/bin:/Users/xgqfrms-mm/.nvm/versions/node/v18.12.0/bin:/usr/local/opt/mongodb-community@4.2/bin:/Users/xgqfrms-mm/.nvm/versions/node/v18.12.0/bin:/bin:/usr/bin:/usr/local/bin:/Users/xgqfrms-mm/.nvm/versions/node/v18.12.0/bin:/bin:/usr/bin:/usr/local/bin:/Users/xgqfrms-mm/.nvm/versions/node/v18.12.0/bin:/usr/local/opt/mongodb-community@4.2/bin:/Users/xgqfrms-mm/.nvm/versions/node/v18.12.0/bin:/bin:/usr/bin:/usr/local/bin:/Users/xgqfrms-mm/.nvm/versions/node/v18.12.0/bin:/Library/Frameworks/Python.framework/Versions/3.11/bin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/Apple/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/usr/local/mysql/bin/:/usr/local/mysql/bin/

(? 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!

shell 启动配置文件

shell 的启动方式不同,加载启动配置文件就不同 (数量/顺序)

  1. /etc/profile
  2. $HOME/.bash_profile
  3. $HOME/.bashrc
  4. $HOME/.bash_login
  5. $HOME/.profile
$ cat /etc/profile
# System-wide .profile for sh(1)

if [ -x /usr/libexec/path_helper ]; then
	eval `/usr/libexec/path_helper -s`
fi

if [ "${BASH-no}" != "no" ]; then
	[ -r /etc/bashrc ] && . /etc/bashrc
fi

refs



©xgqfrms 2012-2021

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 ?️,侵权必究⚠️!