Variable and function syntax
FOO=bar
VAR2=/home/user/docs
function yo()
{
echo "yo"
}List of all exported shell variables
Command used to allow child sessions to use env vars
Show environment vars
env
List shell settings or shell vars and funcs for the session
set
set -x/set +x- turn on/off she debugging
set -f -enable file globbing
set +f -disable file globbing
Remove var or function
unset -f yo
unset FOO
Display shell options
Modify shell options
shopt
shopt -s histreedit- set
shopt -u histreedit -unset
Locate an app file listed in $PATH
which bash
Determine if something is a function, file, alias, built-in or keyword
type cd=> function
type type=> built-in
Interactive login shell
/etc/profile=>/etc/profile.d/*
info from /etc/profile.d/ returns back to /etc/profile=>~/.bash_profile or ~/.profile =>~/.bashrc=>/etc/bashrc
Interactive non-login shell
~/.bashrc=>/etc/bashrc
First file read on login session. Sets env vars such as PATH, umask, bash history controls
/etc/profile
Configs of system-wide functions and aliases
/etc/bashrc
Templates of files that are added when an account is created
/etc/skel
Legacy ~/.bash_profile file
~/.bash_login
Set alias
vim ~/.bashrc
alias ll=”ls -la”
source ~/.bashrc or . ~/.bashrc
Run command from history
!56
run 56 line from history
Location of history
~/.bash_history
Env var keeping size of line in .bash_history
HISTFILESIZE
Man pages
Get pid of current bash process
echo $$
Expand escaping chars to be printed in echo example
echo -e “This is an example of \n escaping chars \t\t !!”
Exit codes
0= no issue
!=0 -issue
1. execute any command
2. echo $? to get exit value
Conditional statements
!/bin/bash
var=$1
if [ $var == 1 ]
then
echo "$var eq one"
fi
if [ $var -ne 2 ] && [ $var -ne 1 ]
then
echo "$var equals 3"
ficase $var in
1)
echo “in this case the positional parameter eq 1”
;;
2)
echo “in this case the positional parameter eq 2”
;;
3)
echo “in this case the positional parameter eq 3”
;;
esac
Loops
!/bin/bash
for i in {1..5}
do
echo $i
done
echo 'for loop is complete'
counter=6
while [ $counter -le 9 ]
do
echo $counter
counter=$[ $counter+1 ]
doneecho ‘while loop is complete’
number=10
until [ $number -eq 15 ]
do
echo $number
((number++))
done