Why does changing $PATH affect child shells, but changing $foo not?

Motte001
$ 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 exported. Why?

Stephen Harris

There are really two types of variable:

  1. Environment variables
  2. Shell variables

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] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Why does the wildcard * not work when changing directories?

분류에서Dev

Angularjs - changing location.path from modal

분류에서Dev

Why does Excel treat long numeric strings as scientific notation even after changing cell format to text

분류에서Dev

Why does all Word.Range object change when changing one of them?

분류에서Dev

Why my char* is changing without reason?

분류에서Dev

After changing hostname, Ubuntu does not recognize the name

분류에서Dev

Does changing metadata reset your ratings? (iOS)

분류에서Dev

Does changing alpha value change text?

분류에서Dev

After changing hostname, Ubuntu does not recognize the name

분류에서Dev

Why are interactive shells on OSX login shells by default?

분류에서Dev

os.system() inline expression is not changing my PATH

분류에서Dev

Why is iterating through my pandas data changing the values?

분류에서Dev

Why is this array of chars changing value when another function is called?

분류에서Dev

why changing source of image , execute my inline javascript

분류에서Dev

Why is overriding the LANG environment variable not changing the language for me?

분류에서Dev

Why changing DNS won't bypass the internet censorship

분류에서Dev

Changing mixins.scss does not change the CSS files importing it

분류에서Dev

Collision with walls does not apply when changing ball location

분류에서Dev

Why does the printf function affect my speller program?

분류에서Dev

why does the addDomain method affect the Domain's SuperClass

분류에서Dev

Why does -a in "#!/bin/sh -a" affect sed and "set -a" doesn't?

분류에서Dev

Why does $0=/bin/foo when /bin is a symlink

분류에서Dev

Why does SVG path curve get filled?

분류에서Dev

Changing the form and arrangement of the workspaces

분류에서Dev

Changing the default program for an application

분류에서Dev

loop for changing file endings

분류에서Dev

Changing default rails in ubuntu

분류에서Dev

changing html to C#

분류에서Dev

Changing Range Breaks Sub

Related 관련 기사

  1. 1

    Why does the wildcard * not work when changing directories?

  2. 2

    Angularjs - changing location.path from modal

  3. 3

    Why does Excel treat long numeric strings as scientific notation even after changing cell format to text

  4. 4

    Why does all Word.Range object change when changing one of them?

  5. 5

    Why my char* is changing without reason?

  6. 6

    After changing hostname, Ubuntu does not recognize the name

  7. 7

    Does changing metadata reset your ratings? (iOS)

  8. 8

    Does changing alpha value change text?

  9. 9

    After changing hostname, Ubuntu does not recognize the name

  10. 10

    Why are interactive shells on OSX login shells by default?

  11. 11

    os.system() inline expression is not changing my PATH

  12. 12

    Why is iterating through my pandas data changing the values?

  13. 13

    Why is this array of chars changing value when another function is called?

  14. 14

    why changing source of image , execute my inline javascript

  15. 15

    Why is overriding the LANG environment variable not changing the language for me?

  16. 16

    Why changing DNS won't bypass the internet censorship

  17. 17

    Changing mixins.scss does not change the CSS files importing it

  18. 18

    Collision with walls does not apply when changing ball location

  19. 19

    Why does the printf function affect my speller program?

  20. 20

    why does the addDomain method affect the Domain's SuperClass

  21. 21

    Why does -a in "#!/bin/sh -a" affect sed and "set -a" doesn't?

  22. 22

    Why does $0=/bin/foo when /bin is a symlink

  23. 23

    Why does SVG path curve get filled?

  24. 24

    Changing the form and arrangement of the workspaces

  25. 25

    Changing the default program for an application

  26. 26

    loop for changing file endings

  27. 27

    Changing default rails in ubuntu

  28. 28

    changing html to C#

  29. 29

    Changing Range Breaks Sub

뜨겁다태그

보관