BSalunke
BSalunke

Reputation: 11727

What is the difference while exporting the env variable in linux from a file?

I'm working on Linux. What is the difference between following export statements of two environmental variables?

export PATH=/opt/rh/devtoolset-2/root/usr/bin${PATH:+:${PATH}}
export MANPATH=/opt/rh/devtoolset-2/root/usr/share/man:$MANPATH

Upvotes: 0

Views: 215

Answers (2)

Alfe
Alfe

Reputation: 59426

The use of the syntax ${PATH:+:$PATH} (used for expanding $PATH) takes care of the (pathological) case that $PATH is empty (or unset). In this case, the result will be empty, otherwise it will be :$PATH, ensuring that the result of the expansion will be either /opt/rh/devtoolset-2/root/usr/bin alone (in the pathological case) or /opt/rh/devtoolset-2/root/usr/bin:$PATH in the typical case.

The expansion of $MANPATH does not take care of the pathological case, so in case $MANPATH was empty or unset, the result will be /opt/rh/devtoolset-2/root/usr/share/man:, containing a stray colon at the end.

Upvotes: 1

Jayesh Bhoi
Jayesh Bhoi

Reputation: 25875

In linux/unix PATH is standard environment variable for searching required executable and other files from any point. So your shell searches through when you enter a command. Using first command.To modify path it depend on shells like Bash, Sh, Ksh shell.

 export PATH=/opt/rh/devtoolset-2/root/usr/bin${PATH:+:${PATH}}

It append your given path to standard Linux env path.

While in second one

export MANPATH=/opt/rh/devtoolset-2/root/usr/share/man:$MANPATH

you create your custom path name (MANPATH) and exported it so you can now access whole path using $MANPATH variable.

Note that above path modification is temporary . for permanent changes you need to modify ~/.profile file for sh and ksh shell, or ~/.bash_profile file for bash shell.

For example in BASH shall

echo 'export PATH=$PATH:/usr/local/bin'  >> ~/.bash_profile

Upvotes: 0

Related Questions