xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

Linux shell 环境变量 All In One

Linux shell 环境变量 All In One

环境变量

  1. 全局环境变量,system wide environment 系统级环境变量/ environment variables
    $ export NVM_USER_ENV=xgqfrms
  2. 局部环境变量,local wide environment 用户自定义环境变量 / shell variables
    $ NVM_USER_ENV=xgqfrms ./test.sh / NVM_USER_ENV=xgqfrms
  3. 永久环境变量,环境变量配置文件, 每次登录自动加载,并且 export 到全局环境变量
    /etc/environment file
NVM_USER_ENV=xgqfrms
APP_NODE_ENV=dev
# ...

shell 的 3 种启动方式

  1. 登录默认的交互式 shell (parent shell / parent process)
  2. 非登录交互式 shell
  3. 运行一个 shell 脚本产生的非交互式 shell (child shell / child process)

父 shell / 子 shell

  1. 父 shell 与子 shell 的 local shell variables 互相不可以共享, 只能使用自己定义的局部环境变量
  2. 父 shell 与子 shell, 都可以访问 global environment variavles, 且可以使用 export 方式临时覆盖掉同名的全局环境变量

parent shell vs child shell

eric@rpi4b:~ $ printenv NVM_USER_ENV
eric@rpi4b:~ $ printenv | grep NVM_USER_ENV
eric@rpi4b:~ $ printenv | grep NVM_USER
NVM_USER=xgqfrms_pi4b
eric@rpi4b:~ $ printenv NVM_USER
xgqfrms_pi4b
eric@rpi4b:~ $ env NVM_USER
env: “NVM_USER”: 没有那个文件或目录
eric@rpi4b:~ $ export NVM_USER_ENV=eric_env
eric@rpi4b:~ $ printenv | grep NVM_USER
NVM_USER_ENV=eric_env
NVM_USER=xgqfrms_pi4b
eric@rpi4b:~ $ export NVM_USER_ENV=eric_env_new
eric@rpi4b:~ $ printenv | grep NVM_USER
NVM_USER_ENV=eric_env_new
NVM_USER=xgqfrms_pi4b
eric@rpi4b:~ $ bash
fix vim bug ✅
eric@rpi4b:~ $ echo $NVM_USER
xgqfrms_pi4b
eric@rpi4b:~ $ printenv | grep NVM_USER
NVM_USER_ENV=eric_env_new
NVM_USER=xgqfrms_pi4b
eric@rpi4b:~ $ echo $NVM_USER_ENV
eric_env_new
eric@rpi4b:~ $ export NVM_USER_ENV=eric_env_child
eric@rpi4b:~ $ echo $NVM_USER_ENV
eric_env_child
eric@rpi4b:~ $ exit
exit
eric@rpi4b:~ $ printenv | grep NVM_USER
NVM_USER_ENV=eric_env_new
NVM_USER=xgqfrms_pi4b
eric@rpi4b:~ $ 
eric@rpi4b:~ $ printenv | grep NVM_USER
NVM_USER_ENV=eric_env_new
NVM_USER=xgqfrms_pi4b

eric@rpi4b:~ $ printenv | grep NVM_USER
NVM_USER_ENV=eric_env_new
NVM_USER=xgqfrms_pi4b
eric@rpi4b:~ $ bash
fix vim bug ✅
eric@rpi4b:~ $ echo $NVM_USER_ENV
eric_env_new
eric@rpi4b:~ $ export NVM_USER_ENV_CHILD=child_shell_env
eric@rpi4b:~ $ echo $NVM_USER_CHILD

eric@rpi4b:~ $ echo $NVM_USER_ENV_CHILD
child_shell_env
eric@rpi4b:~ $ exit
exit
eric@rpi4b:~ $ echo $NVM_USER_ENV_CHILD

eric@rpi4b:~ $ printenv | grep NVM_USER
NVM_USER_ENV=eric_env_new
NVM_USER=xgqfrms_pi4b
eric@rpi4b:~ $ 



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

设置系统环境变量的方式

  1. 通过 shell 命令行 (临时性的环境变量,退出 shell 命令行,会自动销毁 )
  2. 通过 shell script 脚本文件 (临时性的环境变量,退出 shell 命令行,会自动销毁 )
  3. 通过 shell 启动配置文件,如 /etc/profile (持久化 的环境变量,每次启动 shell 都会通过加载启动配置文件自动重新创建 )

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

# 设置系统环境变量, 全大写字母
# key=value 等号两边不可以有空格⚠️
$ export GITHUB_ENV_USERNAME=xgqfrms

$ echo $GITHUB_ENV_USERNAME

__CFBundleIdentifier=com.apple.Terminal
TMPDIR=/var/folders/by/24_sdryj5sv4qvxcyjfnn64c0000gn/T/
XPC_FLAGS=0x0
TERM=xterm-256color
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:/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/
SHLVL=2
PWD=/Users/xgqfrms-mm
OLDPWD=/Users/xgqfrms-mm
ZSH=/Users/xgqfrms-mm/.oh-my-zsh
PAGER=less
LESS=-R
NVM_DIR=/Users/xgqfrms-mm/.nvm
NVM_CD_FLAGS=-q
NVM_BIN=/Users/xgqfrms-mm/.nvm/versions/node/v18.12.0/bin
GITHUB_ENV_USERNAME=xgqfrms
LC_CTYPE=UTF-8
_=/usr/bin/env

image

局部环境变量

⚠️ 没有单独查看当前局部环境变量的命令
set 查看所有环境变量,会对变量按照字母大小和字母顺序进行排序

# 查看当前用户拥有的所有环境变量 (`局部` shell 环境变量 + 全局`系统`环境变量)
$ set

set
'!'=0
'#'=0
'$'=1664
'*'=(  )
0=zsh
'?'=0
@=(  )
ARGC=0
BG
CDPATH=''
COLUMNS=90
CPUTYPE=x86_64
EGID=20
EUID=501
FG
FIGNORE=''
FPATH=/Users/xgqfrms-mm/.oh-my-zsh/plugins/git:/Users/xgqfrms-mm/.oh-my-zsh/functions:/Users/xgqfrms-mm/.oh-my-zsh/completions:/usr/local/share/zsh/site-functions:/usr/share/zsh/site-functions:/usr/share/zsh/5.9/functions
FUNCNEST=700
FX
GID=20
GITHUB_ENV_USERNAME=xgqfrms
HISTCHARS='!^#'
HISTCMD=6710
HISTFILE=/Users/xgqfrms-mm/.zsh_history
HISTSIZE=50000
HOME=/Users/xgqfrms-mm
HOST=xgqfrms-mm.local
IFS=$' \t\n\C-@'
KEYBOARD_HACK=''
KEYTIMEOUT=40
LC_CTYPE=UTF-8
LESS=-R
LINENO=10
LINES=69
LISTMAX=100
LOGNAME=xgqfrms-mm
MACHTYPE=x86_64
MAILCHECK=60
MAILPATH=''
MANPATH=''
MATCH=m
MBEGIN=1
MEND=1
MODULE_PATH=/usr/lib/zsh/5.9
NULLCMD=cat
NVM_BIN=/Users/xgqfrms-mm/.nvm/versions/node/v18.12.0/bin
NVM_CD_FLAGS=-q
NVM_DIR=/Users/xgqfrms-mm/.nvm
OLDPWD=/Users/xgqfrms-mm
OPTARG=''
OPTIND=1
OSTYPE=darwin22.0
PAGER=less
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/
PPID=73157
PROMPT=$'%(?:%{\C-[[01;32m%}➜ :%{\C-[[01;31m%}➜ ) %{$fg[cyan]%}%c%{$reset_color%} $(git_prompt_info)'
PROMPT2='%_> '
PROMPT3='?# '
PROMPT4='+%N:%i> '
PS1=$'%(?:%{\C-[[01;32m%}➜ :%{\C-[[01;31m%}➜ ) %{$fg[cyan]%}%c%{$reset_color%} $(git_prompt_info)'
PS2='%_> '
PS3='?# '
PS4='+%N:%i> '
PSVAR=''
PWD=/Users/xgqfrms-mm
RANDOM=11289
READNULLCMD=more
SAVEHIST=10000
SCREEN_NO=''
SECONDS=1120
SECURITYSESSIONID=186a6
SHELL=/bin/zsh
SHELL_SESSION_DID_HISTORY_CHECK=1
SHELL_SESSION_DID_INIT=1
SHELL_SESSION_DIR=/Users/xgqfrms-mm/.zsh_sessions

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

# var✅ 没有 $, 赋值、修改等操作时候
$ github_env_key=value
$ github_env_key="space in value"

# $var✅ 有 $, 读取使用时候
$ echo $github_env_key

父 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...then...fi 条件控制语句
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

set & unset

       set [ {+|-}options | {+|-}o [ option_name ] ] ... [ {+|-}A [ name ] ]
           [ arg ... ]
              Set the options for the shell and/or set the positional
              parameters, or declare and set an array.  If the -s option is
              given, it causes the specified arguments to be sorted before
              assigning them to the positional parameters (or to the array
              name if -A is used).  With +s sort arguments in descending
              order.  For the meaning of the other flags, see zshoptions(1).
              Flags may be specified by name using the -o option. If no option
              name is supplied with -o, the current option states are printed:
              see the description of setopt below for more information on the
              format.  With +o they are printed in a form that can be used as
              input to the shell.

              If the -A flag is specified, name is set to an array containing
              the given args; if no name is specified, all arrays are printed
              together with their values.

              If +A is used and name is an array, the given arguments will
              replace the initial elements of that array; if no name is
              specified, all arrays are printed without their values.

              The behaviour of arguments after -A name or +A name depends on
              whether the option KSH_ARRAYS is set.  If it is not set, all
              arguments following name are treated as values for the array,
              regardless of their form.  If the option is set, normal option
              processing continues at that point; only regular arguments are
              treated as values for the array.  This means that

                     set -A array -x -- foo

              sets array to `-x -- foo' if KSH_ARRAYS is not set, but sets the
              array to foo and turns on the option `-x' if it is set.

              If the -A flag is not present, but there are arguments beyond
              the options, the positional parameters are set.  If the option
              list (if any) is terminated by `--', and there are no further
              arguments, the positional parameters will be unset.

              If no arguments and no `--' are given, then the names and values
              of all parameters are printed on the standard output.  If the
              only argument is `+', the names of all parameters are printed.

              For historical reasons, `set -' is treated as `set +xv' and `set
              - args' as `set +xv -- args' when in any other emulation mode
              than zsh's native mode.


$ man zshbuiltins
# $ man zshbuiltins | grep set

# Unknown locale, assuming C
$ locale
LANG=""
LC_COLLATE="C"
LC_CTYPE="UTF-8"
LC_MESSAGES="C"
LC_MONETARY="C"
LC_NUMERIC="C"
LC_TIME="C"
LC_ALL=

# fix
$ export LANG="en_US.UTF-8"
$ locale
LANG="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_CTYPE="UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_ALL=

fix solution .bashrc

# 写入到用户登录启动配置文件中
$ sudo vim .bashrc
$ cat .bashrc

# fix: vim 无法显示 👻 emoji & 中文 bug
export LC_ALL="zh_CN.UTF-8"

echo "fix vim bug ✅"


# echo varibale
export NVM_USER=xgqfrms_pi4b

# alias
alias vcmt='vcgencmd measure_temp'

https://stackoverflow.com/questions/77058657/why-both-the-set-and-unset-commands-dont-work-as-expected-in-my-macos-terminal#comment135847407_77058657

https://pubs.opengroup.org/onlinepubs/007904875/utilities/set.html

       unset [ -fmv ] name ...
              Each named parameter is unset.  Local parameters remain local
              even if unset; they appear unset within scope, but the previous
              value will still reappear when the scope ends.

              Individual elements of associative array parameters may be unset
              by using subscript syntax on name, which should be quoted (or
              the entire command prefixed with noglob) to protect the
              subscript from filename generation.

              If the -m flag is specified the arguments are taken as patterns
              (should be quoted) and all parameters with matching names are
              unset.  Note that this cannot be used when unsetting associative
              array elements, as the subscript will be treated as part of the
              pattern.

              The -v flag specifies that name refers to parameters. This is
              the default behaviour.

              unset -f is equivalent to unfunction.

https://pubs.opengroup.org/onlinepubs/007904875/utilities/unset.html

refs



©xgqfrms 2012-2021

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

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


posted @ 2023-09-08 11:48  xgqfrms  阅读(47)  评论(0编辑  收藏  举报