Environment variables. Difference set and env commands. What is env, preintenv in bash?
Environment variables are set for the current shell and passed down to any processes or child shells that are created. They are accustomed to providing data to processes that are launched from the active shell.
Displayed using env or printenv commands.
The shell in which they were set or defined is the only place where shell variables can be found.
Displayed using set command.
env and printenv commands:
ubuntu:~# env
SHELL=/bin/bash
…
ubuntu:~# printenv
SHELL=/bin/bash
…
ubuntu:~# printenv SHELL LANG
/bin/bash
en_US.UTF-8
bashrc configure environment variable for user:
ubuntu:~# vim .bashrc
export PATH=$PATH:~/Bash_scripts
ubuntu:~# source ~/.bashrc
ubuntu:~# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
:/usr/local/games:/snap/bin:/root/Bash_scripts
After setting path, we can run scripts with just name:
ubuntu:~# ls Bash_scripts/
bash_variables.sh first_scripts.sh test_py
ubuntu:~# test_py
3.8.10 (default, Jun 22 2022, 20:18:18)
[GCC 9.4.0]
ubuntu:~# bash bash_variables.sh 1 2 3
Inputs: 1 2 3
Total: 3
/etc/profile, /etc/bash.bashrc and /etc/environment files:
ubuntu:~# ls /etc/profile /etc/bash.bashrc /etc/environment
/etc/bash.bashrc /etc/environment /etc/profile
Conclusion
User configuration file: ~/.bashrc. System-wide configuration file /etc/bash.bashrc and /etc/profile.