Last edit: October 01, 2017 09:52:09 AM CDT> List of all cheatsheets ALIAS="`goes here`" logger is your friend Order of precedence: aliases, functions, built-ins commands, scripts and programs found in PATH Profiles .bash_profile (aka .bash_login and .profile): Read and executed by the login shell. .bashrc: Read by new shells/subshells. Environmental file. OS X Terminal always launches login shells, so .bash_profile is always called. Unofficial strict mode #!/bin/bash set -euo pipefail IFS=$'\n\t' Actions ^W = erase word ^U = erase line ^D = end of input ^C = interrupt ^Z = suspend ^\ = quit Customize prompt PS1="" \h = short hostname \d = date \t = time in hh:mm:ss \u = current user \w = current working directory \W = basename of current working directory I/O Redirection > file = direct standard output to file < file = take standard input from file <> file = use file for both standard input and output << file = here-file &> = direct standard output and error to file Iterations, Functions and Script Best Practices Function syntax function function-name { commands } Command substitution $(command) not `command` Arithmetic expansion $((expression)) e.g. echo $(([given answer] - 99)) equals the number of problems Iteration For loop for f in /path/to/folder for f in path do cmd done Does something X number of times (( i=1; i<=5; i++ )) i=1 • Initialize the value of the loop variable i to 1 • As long as the condition i<=5; is True, the loop will continue to run. • i++ (shorthand for i = i + 1) Tests && = and || = or (sort of else) [[ ]] works too Does this exist? [ -e /path/to/file ]; Does this file exist? [ -f /path/to/folder ]; Does this folder exist? [ -d /path/to/folder ]; Is this executable? [ -x /path/to/binary ]; Is this file newer than or older than this file? [ -x /path/to/binary ]; Conditional check for file or folder [ -f /path/to/file ] && echo "if found, then say exists" || echo "if not, then say does not exists" Do Stuff Cut range cut -c #-# Don't grep before awk awk '/[term]/' Set awk field separator awk -F [character to use, including a space, e.g. : , /] Print a particular number line in a listing sed -n '[number]p', e.g. ls -al | sed -n '2p' Print all lines between two matching terms sed -n '/[term 1]/,/[term 2]/p', e.g. ls -al ~ | sed -n '/Library/,/Sites/p' Simple find and replace sed -e "s/[character to find]/[character to replace it with/g' sed 's/this/that/g' Replace particular instances of a string with a new string sed 's/this/that/[number place of instance]', e.g. sed 's/this/that/3' Regular Expressions Find all variations preceding a particular character e.g. /Appl*/ Find all variations that match a character in a specific location e.g. /Appl./ Find an optional match preceding a particular character e.g. /Appl?/ Find one or more matches preceding the character e.g. /Appl+/ Find a specific match e.g. /Apple/ Find a specific match at the beginning of a line e.g /^Apple/ Find a specific match at the end of a line e.g. /Apple$/ Find all matching characters e.g. /[123]/ or /[aeiou]/ or /[AEIOU]/ Find all matching characters that are not part of the set (select the inverse) e.g. /[^123]/ or /[^aeiou]/ or /[^AEIOU]/