Skip to content

Instantly share code, notes, and snippets.

@slank
Created June 4, 2015 04:41
Show Gist options
  • Save slank/96f47bc6ec0035627b1f to your computer and use it in GitHub Desktop.
Save slank/96f47bc6ec0035627b1f to your computer and use it in GitHub Desktop.
Bash Power Tips Walkthrough
# Variables
FOO=here
FOO="here" # equivalent, always use quotes
BAR=$FOO
BAR="$FOO"
BAR="${FOO}"
BAR='$FOO' # single quotes, does not evaluate the variable
BAZ="$FOOsy" # different variable name
BAZ="${FOO}sy"
# Mnemonic: # (3 on keyboard) is in FRONT and % (5 on keboard) is in BACK
echo "${BAZ%e*}" # delete shortest occurrence at end of string
echo "${BAZ#*e}" # ... at beginning of string
echo "${FOO##*e}" # longest occurrence at beginning
echo "${FOO%%e*}" # longest occurrence at end
echo "${FOO/e/a}" # substitute a for first occurence of e
echo "${FOO//e/a}" # substitute a for all occurrences of e
echo "${FOO/#he/ha}" # if beginning of string matches he, replace it with ha
echo "${FOO/%re/ro}" # if end of string ...
echo "${BAZ:0:2}" # characters 0 through 2
echo "${#BAZ}" # number of characters in variable
# Arithmetic
# Operators: Add(+) Subtract(-) Multiply(*) Divide(/) Modulo(%) Exponent(**)
echo "$((1+2))"
X=1
echo "$((X+1))"
echo "$((${X}+1))"
# Tests
# [ vs [[ (latter includes regex (=~) support)
# -d (is directory), -f (is file), -e (exists), -n (nonempty), -z (zero-length)
# -eq, ==
if [[ "${FOO}" =~ ^he ]]; do
echo "Match"
fi
# Control Structures
for VARIABLE in list of values; do
echo "${VARIABLE}"
done
while true; do
echo date
sleep 1
done
if [[ -n "$FOO" ]]; then
echo "Yep"
fi
while read WORD1 WORD2; do # reads a line from stdin, assigns first two words to variables
echo "1: ${WORD1}, 2: ${WORD2}"
done
while : ; do # infinite loop, ":" is a noop
...
break
done
for FILE in $(ls *.jpeg); do
mv "${FILE}" "${FILE%.jpeg}.jpg"
done
# Functions
# local, shift
my_func () {
local VAR1="$1"; shift
local VAR2="$1"
echo "${VAR1}"
echo "${VAR2}"
}
# Paths
# $BASH_SOURCE, path to the current script
# dirname $SOMEPATH, basename $SOMEPATH
# globbing
# ?[ab]*
# {ab*,bc*}
# Command Substitution
# Stop using backticks!
FOO=`dirname $BASH_SOURCE`
FOO="`dirname $BASH_SOURCE`"
# much better
FOO="$(dirname $(realpath "$BASH_SOURCE"))"
# settings/shopt
#!/bin/bash -x
set -x # print every line before evaluating
set -e # fail on errors
set -o pipefail # fail on errors in pipelines, -o can set many options
set -u # fail when undefined variables are encountered
# Pipelines and IO
# > overwrite file
# >> append to file
# < read from file
# << heredoc
# &0, &1, &2 stdin, stdout, stderr
# exec 6<&0; exec 0</foo/bar; ... ; exec 0<&6 6<&- # rearrange file descriptors
# | pipe stdout, |& pipe stdout+stderr
# <() subshell fifo
# <<< string redirection
# Temporary files
TEMPFILE="$(mktemp)"
trap "rm -f $TEMPFILE" exit
# Winning Combos
# curl -s | jq
# ssh somehost bash < my_local_script.sh
# find -exec
# Interactive
# ctrl-r (history search)
# !! - previous command line
# !$ - last argument of previous command
# !ssh - most recent command beginning with ssh
# host somewebsite.com; ^some^my - run previous command with substitutions
# echo hello my name is matthew; echo !!:1 !!:$ - revise and run last command
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment