It lives exclusively inside the specific shell instance
Child processes cannot see it
# see all shell + environment variablesset# creating shell variableMY_VAR="local"# view shell variableecho $MY_VAR# delete shell variableunset MY_VAR
Environment variables
aka Global/Public variables
These variables are inherited by the child process
A child process cannot alter environment of its parent
# see all environment variablesprintenv# creating an enviroment variableexport MY_VAR="global"# view specific environment variableprintenv MY_VARecho $MY_VAR# see variable in child processbash -c# delete environment variableunset MY_VAR
shell variable gets upgraded to environment variable using export
# create shell variable> DB_USER="admin"# shell variable is not printed> printenv DB_USER# upgrades shell to environment variable> export DB_USER# now the variable can be printed because it is environment variable> printenv DB_USERadmin
once environment variable created it cannot be downgraded to shell variable
# create environment variable> export DB_PORT="5432"# does not create shell variable# only updates existing environment variable> DB_PORT="8080"# the variable does not change to shell variable> printenv DB_PORT8080
Launching program with temporary env variable
using direct inline shell variable
PORT=3000 python script.py
using env
# adding an environment variableenv PORT=3000 python script.py# remove all environment variablesenv -i python script.py# keep only PORT and remove all other environment variablesenv -i PORT=3000 python script.py
List of interesting environment variables
SHELL — user’s default shell program, except in bash where it overwrites on launch
HOME — pathname to home directory
PWD — current working directory
OLDPWD — previous working directory
PATH — A colon-separated list of directories that are searched when we enter the name of a executable program
PS1 — Stands for “Prompt String 1” This defines the contents of the shell prompt
This is shell agnostic and inherited by all processes
The syntax is also different from shell scripting
Global
When user logs in to the system, login shell startup files are loaded
User
When user launches a terminal emulator, it inherits those variables from login shell startup files and then loads non-login shell startup files
Setting up environment variables
Bash shell startup files: man bash
See INVOCATION section
Zsh shell startup files: man zshoptions
Login shell session
when we log into graphical environment or start a virtual console session
Bash shell startup files (First global, then user level)
Global
/etc/profile
User (Short-circuit precedence chain: only the highest available file executes)
~/.bash_profile
~/.bash_login
~/.profile
Bash shell logout files
Global
/etc/bash.bash_logout
User
~/.bash_logout
/etc/profile contains logic to
setup PATH env variable based on user is superuser or not
setup prompt shell
source non-login shell global startup file: /etc/bash.bashrc
read configs from /etc/profile.d directory
~/.profile contains logic to
source non-login shell user startup file: ~/.bashrc
Non-Login shell session
when we launch terminal emulator in GUI
Bash Startup files
Global
/etc/bash.bashrc
User
~/.bashrc
Sourcing script
you can dynamically source a script or for example ~/.bashrc to immediately make the changes to current shell session
# sourcing modified ~/.bashrcsource ~/.bashrc# alternative syntax. ~/.bashrc
PATH environment variable
A colon-separated list of directories that are searched when we enter the name of a executable program
Files
System level defined in /etc/environment
Global level defined in /etc/profile
User level defined in ~/.profile
Do not append PATH variable in ~/.bashrc as it can cause duplicate paths to be added each time a non-login child bash shell is launched
# System level: /etc/environment# Has to be exact paths and cannot append to existing onePATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games# Global Level: /etc/profile or /etc/profile.d/custom.shexport PATH="$PATH:/opt/java/bin"# User level: ~/.profileexport PATH="$HOME/.local/bin:$PATH"
Commands
A command is one of the following
executable program
shell built-in command
shell function
alias
type can be used to detect the command type
> type cdcd is a shell builtin
Shell built-in
# determine if it is shell builtintype cd# help on shell builtinsman builtin
Alias
Create an alias for a command
# viewing all aliasesalias# creating aliasalias l='ls -lah --color=auto --group-directories-first'# viewing aliasalias ltype l# removing aliasunalias l
Shell function
# see all shell functionsdeclare -f # bash + zshfunctions # zsh# creating functionmy_func() { ls -lah "$@"}# ORfunction my_func() { ls -lah "$@"}# viewing function definitiondeclare -f my_func # bashfunctions my_func # zsh# remove function# -f: for removing functionunset -f my_func
Check the command origin
## print all matching pathswhere <command> # can be used in windows as wellwhich -a <command>## exact matchwhich <command># ------- Available to most shells ---------## use type to check the command/shell builtin/alias> type nanonano is /usr/bin/nano> type cd cd is a shell builtin> type llll is an alias for ls -l## Most portable, recommended in scripts## print single match and prints exact pathcommand -v <command>