Skip to content

Instantly share code, notes, and snippets.

@BobCHub
Created February 17, 2018 17:40
Show Gist options
  • Save BobCHub/5c55bb0a2f6bdf1a42e8fb4c8e0c2999 to your computer and use it in GitHub Desktop.
Save BobCHub/5c55bb0a2f6bdf1a42e8fb4c8e0c2999 to your computer and use it in GitHub Desktop.
Bash_Reference2
/**<<<<<<<<< Bash scripting cheatsheet >>>>>>>>**/
/*********** Basic Example ************/
// A simple copy script
#!/bin/bash
cp $1 $2
// Let's verify the copy worked
echo Details for $2
ls -lh $2
#!/usr/bin/env bash
NAME="John"
echo "Hello $NAME!"
/*********** Variables ************/
NAME="John"
echo $NAME
echo "$NAME"
echo "${NAME}!"
/*********** Special Variables ************/
$0 - The name of the Bash script.
$1 - $9 - The first 9 arguments to the Bash script. (As mentioned above.)
$# - How many arguments were passed to the Bash script.
$@ - All the arguments supplied to the Bash script.
$? - The exit status of the most recently run process.
$$ - The process ID of the current script.
$USER - The username of the user running the script.
$HOSTNAME - The hostname of the machine the script is running on.
$SECONDS - The number of seconds since the script was started.
$RANDOM - Returns a different random number each time is it referred to.
$LINENO - Returns the current line number in the Bash script.
/*********** String quotes ************/
NAME="John"
echo "Hi $NAME" #=> Hi John
echo 'Hi $NAME' #=> Hi $NAME
/*********** Conditional execution ************/
git commit && git push
git commit || echo "Commit failed"
/*********** Functions ************/
get_name() {
echo "John"
}
echo "You are $(get_name)"
// ------- Defining functions ------- //
myfunc() {
echo "hello $1"
}
# Same as above (alternate syntax)
function myfunc() {
echo "hello $1"
}
myfunc "John"
// ---- Returning values ------- //
myfunc() {
local myresult='some value'
echo $myresult
}
result=$(myfunc)
// ---- Raising errors ------- //
myfunc() {
return 1
}
if myfunc; then
echo "success"
else
echo "failure"
fi
// ---- Arguments ------- //
$# // Number of arguments
$* // All arguments
$@ // All arguments, starting from first
$1 // First argument
/*********** Shell execution ************/
echo "I'm in $(pwd)"
echo "I'm in `pwd`"
# Same
/*********** Conditionals ************/
// Conditions
[ -z STRING ] // Empty string
[ -n STRING ] // Not empty string
[ NUM -eq NUM ] // Equal
[ NUM -ne NUM ] // Not equal
[ NUM -lt NUM ] // Less than
[ NUM -le NUM ] // Less than or equal
[ NUM -gt NUM ] // Greater than
[ NUM -ge NUM ] // Greater than or equal
[[ STRING =~ STRING ]] // Regexp
(( NUM < NUM )) // Numeric conditions
[ -o noclobber ] // If OPTIONNAME is enabled
[ ! EXPR ] // Not
[ X ] && [ Y ] // And
[ X ] || [ Y ] // Or
// File conditions
[ -e FILE ] // Exists
[ -r FILE ] // Readable
[ -h FILE ] // Symlink
[ -d FILE ] // Directory
[ -w FILE ] // Writable
[ -s FILE ] // Size is > 0 bytes
[ -f FILE ] // File
[ -x FILE ] // Executable
[ FILE1 -nt FILE2 ] // 1 is more recent than 2
[ FILE1 -ot FILE2 ] // 2 is more recent than 1
[ FILE1 -ef FILE2 ] // Same files
// Example
# String
if [ -z "$string" ]; then
echo "String is empty"
elif [ -n "$string" ]; then
echo "String is not empty"
fi
# Combinations
if [ X ] && [ Y ]; then
...
fi
# Regex
if [[ "A" =~ "." ]]
if (( $a < $b ))
if [ -e "file.txt" ]; then
echo "file exists"
fi
/*********** Checking files ************/
-r file // Check if file is readable.
-w file // Check if file is writable.
-x file // Check if we have execute access to file.
-f file // Check if file is an ordinary file (as opposed to a directory, a device special file, etc.)
-s file // Check if file has size greater than 0.
-d file // Check if file is a directory.
-e file // Check if file exists. Is true even if file is a directory.
// Example:
if [ -s file ]
then
#such and such
fi
/*********** Checking strings ************/
s1 = s2 // Check if s1 equals s2.
s1 != s2 // Check if s1 is not equal to s2.
-z s1 // Check if s1 has size 0.
-n s1 // Check if s2 has nonzero size.
s1 // Check if s1 is not the empty string.
// Example:
if [ $myvar = "hello" ] ; then
echo "We have a match"
fi
/*********** Checking numbers ************/
n1 -eq n2 // Check to see if n1 equals n2.
n1 -ne n2 // Check to see if n1 is not equal to n2.
n1 -lt n2 // Check to see if n1 < n2.
n1 -le n2 // Check to see if n1 <= n2.
n1 -gt n2 // Check to see if n1 > n2.
n1 -ge n2 // Check to see if n1 >= n2.
// Example:
if [ $# -gt 1 ]
then
echo "ERROR: should have 0 or 1 command-line parameters"
fi
/*********** Boolean operators: ************/
! not
-a and
-o or
// Example:
if [ $num -lt 10 -o $num -gt 100 ]
then
echo "Number $num is out of range"
elif [ ! -w $filename ]
then
echo "Cannot write to $filename"
fi
// Note that ifs can be nested. For example:
if [ $myvar = "y" ]
then
echo "Enter count of number of items"
read num
if [ $num -le 0 ]
then
echo "Invalid count of $num was given"
else
#... do whatever ...
fi
fi
/*********** I/O Redirection: ************/
pgm > file // Output of pgm is redirected to file.
pgm < file // Program pgm reads its input from file.
pgm >> file // Output of pgm is appended to file.
pgm1 | pgm2 // Output of pgm1 is piped into pgm2 as the input to pgm2.
n > file // Output from stream with descriptor n redirected to file.
n >> file // Output from stream with descriptor n appended to file.
n >& m // Merge output from stream n with stream m.
n <& m // Merge input from stream n with stream m.
<< tag // Standard input comes from here through next tag at start of line.
/*********** Loops ************/
// Basic for loop
for i in /etc/rc.*; do
echo $i
done
// Forever
while true; do
···
done
// Ranges
for i in {1..5}; do
echo "Welcome $i"
done
// Ranges With step size
for i in {5..50..5}; do
echo "Welcome $i"
done
// Reading lines
cat file.txt | while read line; do
echo $line
done
/*********** Pattern Matching: ************/
* // Matches 0 or more characters.
? // Matches 1 character.
[AaBbCc] // Example: matches any 1 char from the list.
[^RGB] // Example: matches any 1 char not in the list.
[a-g] // Example: matches any 1 char from this range.
/*********** Quoting ************/
\c // Take character c literally.
`cmd` // Run cmd and replace it in the line of code with its output.
"whatever" // Take whatever literally, after first interpreting $, `...`, \
'whatever' // Take whatever absolutely literally.
/*********** Arrays ************/
// Defining arrays
Fruits=('Apple' 'Banana' 'Orange')
Fruits[0]="Apple"
Fruits[1]="Banana"
Fruits[2]="Orange"
// Working with arrays
echo ${Fruits[0]} # Element #0
echo ${Fruits[@]} # All elements, space-separated
echo ${#Fruits[@]} # Number of elements
echo ${#Fruits} # String length of the 1st element
echo ${#Fruits[3]} # String length of the Nth element
echo ${Fruits[@]:3:2} # Range (from position 3, length 2)
// Operations
Fruits=("${Fruits[@]}" "Watermelon") # Push
Fruits=( ${Fruits[@]/Ap*/} ) # Remove by regex match
unset Fruits[2] # Remove one item
Fruits=("${Fruits[@]}") # Duplicate
Fruits=("${Fruits[@]}" "${Veggies[@]}") # Concatenate
lines=(`cat "logfile"`) # Read from file
// Iteration
for i in "${arrayName[@]}"; do
echo $i
done
/*********** Options ************/
set -o noclobber # Avoid overlay files (echo "hi" > foo)
set -o errexit # Used to exit upon error, avoiding cascading errors
set -o pipefail # Unveils hidden failures
set -o nounset # Exposes unset variables
// Glob options
set -o nullglob # Non-matching globs are removed ('*.foo' => '')
set -o failglob # Non-matching globs throw errors
set -o nocaseglob # Case insensitive globs
set -o globdots # Wildcards match dotfiles ("*.sh" => ".foo.sh")
set -o globstar # Allow ** for recursive matches ('lib/**/*.rb' => 'lib/a/b/c.rb')
/*********** History ************/
// Commands
history // Show history
shopt -s histverify // Don’t execute expanded result immediately
// Expansions
!$ // Expand last parameter of most recent command
!* // Expand all parameters of most recent command
!-n // Expand nth most recent command
!n // Expand nth command in history
!<command> // Expand most recent invocation of command <command>
// Operations
!!:s/<FROM>/<TO>/ // Replace first occurrence of <FROM> to <TO> in most recent command
!!:gs/<FROM>/<TO>/ // Replace all occurrences of <FROM> to <TO> in most recent command
!$:t // Expand only basename from last parameter of most recent command
!$:h // Expand only directory from last parameter of most recent command
// Slices
!!:n // Expand only nth token from most recent command (command is 0; first param is 1)
!!:n-m // Expand range of tokens from most recent command
!!:n-$ // Expand nth token to last from most recent command
/*********** Miscellaneous ************/
//Numeric calculations
$((a + 200)) // Add 200 to $a
$((RANDOM%=200)) // Random number 0..200
// Inspecting commands
command -V cd
#=> "cd is a function/alias/whatever"
// Trap errors
trap 'echo Error at about $LINENO' ERR
// or
traperr() {
echo "ERROR: ${BASH_SOURCE[1]} at about ${BASH_LINENO[0]}"
}
set -o errtrace
trap traperr ERR
// Source relative
source "${0%/*}/../share/foo.sh"
// Directory of script
DIR="${0%/*}"
// Heredoc
cat <<END
hello world
END
// Reading input
echo -n "Proceed? [y/n]: "
read ans
echo $ans
read -n 1 ans // Just one character
// Subshells
(cd somedir; echo "I'm now in $PWD")
pwd # still in first directory
// Redirection
python hello.py > output.txt # stdout to (file)
python hello.py >> output.txt # stdout to (file), append
python hello.py 2> error.log # stderr to (file)
python hello.py 2>&1 # stderr to stdout
python hello.py 2>/dev/null # stderr to (null)
python hello.py < foo.txt
// Case/switch
case "$1" in
start | up)
vagrant up
;;
*)
echo "Usage: $0 {start|stop|ssh}"
;;
esac
// printf
printf "Hello %s, I'm %s" Sven Olga
#=> "Hello Sven, I'm Olga
// Getting options
while [[ "$1" =~ ^- && ! "$1" == "--" ]]; do case $1 in
-V | --version )
echo $version
exit
;;
-s | --string )
shift; string=$1
;;
-f | --flag )
flag=1
;;
esac; shift; done
if [[ "$1" == '--' ]]; then shift; fi
// Special variables
$? Exit status of last task
$! PID of last background task
$$ PID of shell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment