$ unset foo
$ unset bar
$ echo $foo
$ echo $bar
$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
$ foo=a
$ bar=b
$ export bar
$ echo $foo
a
$ echo $bar
b
$ PATH=
$ echo $PATH
$ /bin/bash
bash: lesspipe: No such file or directory
bash: dircolors: No such file or directory
bash: ls: No such file or directory
$ echo $foo
$ echo $bar
b
$ echo $PATH
$
As we can see, changing $PATH
affects the subshell, whereas another variable needs to be export
ed. Why?
There are really two types of variable:
To make things more complicated, they both look the same, and a shell variable can be converted to an environment variable with the export
command.
The env
command will show the current set of environment variables.
$ myvar=100
$ env | grep myvar
$ export myvar
$ env | grep myvar
myvar=100
Variables can also be temporarily exported for the life of a command.
$ env | grep anothervar
$ anothervar=100 env | grep anothervar
anothervar=100
$ env | grep anothervar
$
When the shell starts up it inherits a number of environment variables (which may be zero).
Startup scripts (eg .bash_profile
, .bashrc
, files in the /etc
directory) can also set and export variables.
Finally the shell, itself, may set a default number to environment variables if the environment is empty. e.g.
$ PATH=foo /bin/bash -c 'echo $PATH'
foo
$ PATH= /bin/bash -c 'echo $PATH'
$ unset PATH
$ /bin/bash -c 'echo $PATH'
/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다