Environment Variable

最後更新: 2023-03-22

目錄

  • /etc/profile 與 /etc/environment
  • CLI - env
  • PATH
  • Unsetting an Environment Variable

/etc/profile 與 /etc/environment

 

/etc/environment

system-wide environment variable settings. It is not a basic script file.

It is not part of POSIX, it belongs to PAM(pam_env.so).

(programs compiled with PAM support are able to use it)

It is used for setting variables for programs which are usually not started from a shell.

You can see the programs using /etc/environment with

grep -l pam_env /etc/pam.d/*

Note:

Variable expansion does not work(PATH=$PATH)

Remark

Setting "PATH" in /etc/environment might not work, since the global startup scripts for the shell might rewrite them.

(/etc/profile & /etc/bash.bashrc & /etc/profile.d/*)

Example

echo PATH="/opt/node/bin:/opt/git/bin:/sbin:/bin:/usr/sbin:/usr/bin" > /etc/environment

/etc/profile

This file gets executed whenever a bash login shell is entered

/etc/profile is only read for login shells;

/etc/bash.bashrc

rc file for Script

 


CLI - env

 

run a program in a modified environment

env [Opts]... [-] [NAME=VALUE]... [COMMAND [ARG]...]

沒有參數執行它時就會顯示當前的 environment variable

Opts

  • -i, --ignore-environment      # start with an empty environment
  • -u, --unset=NAME              # remove variable from the environment

ie.

[1]

env CFLAGS="-O0" ./configure

[2]

myPATH='PATH=/opt/node/bin:/opt/git/bin:/sbin:/bin:/usr/sbin:/usr/bin'

env $myPATH env

 


PATH

 

優先次序: 左 > 右

自定某 Program 的 Path

/etc/profile.d/mynpm.sh

#!/bin/bash
PATH=/opt/node/bin:$PATH

chmod 755 /etc/profile.d/mynpm.sh

 


Unsetting an Environment Variable

 

unset MyVar