Environment

Environment#

In a Linux shell, the environment defines many aspects of shell behavior. This page discusses commands for configuring the environment, as well as the specific behaviors associated with different parameters.

Variables#

You can define variables in the current shell session using the syntax <variable_name>=<value>. To access a variable, use the syntax $variable_name. These variables will be substituted in the current command.

There is a special type of variable widely used in practice — environment variables. Environment variables are variables inherited by nested shells. The environment passed to any executed command includes the shell’s initial environment. Use the syntax export <variable_name>=<value> to set an environment variable.

Check:


The following code demonstrates defining a variable and substituting its value into the echo command.

MY_VAR=10
echo $MY_VAR
10

The following cell defines an environment variable and a regular variable, then attempts to access both from a nested bash shell:

export env_var="environment"
just_var="local"

bash -c "echo \"\$just_var \$env_var\""
 environment

As a result, only the value for the environment variable exists in the nested shell.

Path#

In Linux, PATH is an environment variable that specifies where the system should look for commands to be executed. Each path is separated by a colon (:). When you type any command in the terminal, the system searches through each path listed in the PATH variable for the corresponding command.

For more details check Path page.


The following cell displays the PATH value of the environment the notebook is running in.

echo $PATH
/root/.vscode-server/bin/cd4ee3b1c348a13bafd8f9ad8060705f6d4b9cba/bin/remote-cli:/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

Consider the contents of /usr/local/bin, one of the folders typically listed in the PATH.

ls /usr/local/bin
2to3	   ipython	       jupyter-troubleshoot  pygmentize
2to3-3.12  ipython3	       pip		     python
debugpy    jupyter	       pip3		     python-config
filetype   jupyter-kernel      pip3.12		     python3
idle	   jupyter-kernelspec  pydoc		     python3-config
idle3	   jupyter-migrate     pydoc3		     python3.12
idle3.12   jupyter-run	       pydoc3.12	     python3.12-config

Some of the names correspond to commands commonly used for managing Jupyter.

Script update#

Use the source <file_name> command to update environment from script. The . <file_name> command is typically equivalent.


The following cell creates a script that sets the var="hello world" environment variable.

rm -rf /tmp/source
mkdir -p /tmp/source
cat << EOF > /tmp/source/script.sh
export var="hello world"
EOF

In case script is executed from bash, the environment still doesn’t recognize the variable.

bash /tmp/source/script.sh
echo $var

However, if the script is executed with the source command the corresponding var variable appears in the environment.

source /tmp/source/script.sh
echo $var
hello world