Bash will empty the oldpwd inherited from the parent process.

Source: Internet
Author: User

Even if Bash does not inherit any environment variables from the parent process, bash will create three environment variables, respectively:

$ env-i BASH-C Export

Declare-x oldpwd

Declare-x pwd= "/"

Declare-x shlvl= "1"

Shlvl

If the parent process passes the SHLVL environment variable, Bash accumulates 1 on that value and assigns an initial value of 1 if there is no pass:

$ env-i shlvl=22 bash-c ' echo $SHLVL '

23

$ env-i bash-c ' echo $SHLVL '

1

Pwd

If the parent process passes the PWD environment variable, and the value of the environment variable is the path to the current directory and is an absolute path, bash inherits the value directly, otherwise bash calls the GETCWD C function to get the current working directory assigned to PWD.

$ env-i pwd=/bash-c ' echo $PWD '

/

$ env-i pwd=/etc bash-c ' echo $PWD ' # is not the path of the current directory

/

$ env-i pwd=. Bash-c ' echo $PWD ' # is the path to the current directory, but is a relative path.

/

Oldpwd

If the parent process passes the OLDPWD environment variable, Bash empties the value of the variable directly.

$ env-i oldpwd=/etc bash-c ' echo $OLDPWD '

$ env-i oldpwd=/etc ksh-c ' echo $OLDPWD '

/etc

$ env-i oldpwd=/etc csh-c ' echo $OLDPWD '

/etc

$ env-i oldpwd=/etc zsh-c ' echo $OLDPWD '

/

It can be seen that unlike Bash,ksh and csh inherit that value directly, and zsh overwrites the value of the inherited OLDPWD variable with the value of the current working directory.

Someone raised the question on Bug-bash. Http://lists.gnu.org/archive/html/bug-bash/2015-11/msg00115.html,Bash the author says it will try to change this behavior in the next version, But not copy Ksh and csh practice, Ksh and CSH simply inherit the value of oldpwd, do not judge that the value is not a valid directory path of existence:

$ env-i oldpwd=2333 ksh-c ' echo $OLDPWD '

2333

$ env-i oldpwd=2333 csh-c ' echo $OLDPWD '

2333

The Bash author says he is prepared to judge if it is a legal directory path to inherit.

Bash will empty the oldpwd inherited from the parent process.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.