Data in Shell

  • Shell variables
  • Environment variables
  • Aliases
  • Shell Functions
  • topics:
    • use of set command

Shell variables

  • aka Local/Private variables
  • Data placed there by current instance of shell
  • It lives exclusively inside the specific shell instance
  • Child processes cannot see it
# see all shell + environment variables
set
 
# creating shell variable
MY_VAR="local"
 
# view shell variable
echo $MY_VAR
 
# delete shell variable
unset 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 variables
printenv
 
# creating an enviroment variable
export MY_VAR="global"
 
# view specific environment variable
printenv MY_VAR
echo $MY_VAR
 
# see variable in child process
bash -c 
 
# delete environment variable
unset 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_USER
admin
 
  • 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_PORT
8080

Launching program with temporary env variable

  • using direct inline shell variable
PORT=3000 python script.py
  • using env
# adding an environment variable
env PORT=3000 python script.py
 
# remove all environment variables
env -i python script.py
 
# keep only PORT and remove all other environment variables
env -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
  • TERM — Terminal type
  • LANG — character set and collation order
  • TZ — Timezone
  • USER — current user name

Types of Environment variables

  • System
    • Defined by /etc/environment
    • 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 ~/.bashrc
source ~/.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 one
PATH=/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.sh
export PATH="$PATH:/opt/java/bin"
 
# User level: ~/.profile
export 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 cd
cd is a shell builtin

Shell built-in

# determine if it is shell builtin
type cd
 
# help on shell builtins
man builtin

Alias

  • Create an alias for a command
# viewing all aliases
alias
 
# creating alias
alias l='ls -lah --color=auto --group-directories-first'
 
# viewing alias
alias l
type l
 
# removing alias
unalias l

Shell function

# see all shell functions
declare -f # bash + zsh
functions # zsh
 
# creating function
my_func() {
    ls -lah "$@"
}
# OR
function my_func() {
    ls -lah "$@"
}
 
# viewing function definition
declare -f my_func # bash
functions my_func # zsh
 
# remove function
# -f: for removing function
unset -f my_func

Check the command origin

## print all matching paths
where <command> # can be used in windows as well
which -a <command>
 
## exact match
which <command>
 
 
# ------- Available to most shells ---------
 
## use type to check the command/shell builtin/alias
> type nano
nano is /usr/bin/nano
> type cd 
cd is a shell builtin
> type ll
ll is an alias for ls -l
 
## Most portable, recommended in scripts
## print single match and prints exact path
command -v <command>