Last edit: May 27, 2019 09:34:42 AM CDT>
List of all cheatsheets
See also *nix
ALIAS="`goes here`"
logger is your friend
Order of precedence: aliases, functions, built-ins commands, scripts and programs found in PATH
How long does it take a command to run?
time [cmd]
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.
Dump aliases
alias
Dump environment
env
Dump shell variables
set
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
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
For loop
for f in path
do
cmd
done
While loop
while true; do
cmd
done
Count loop
for i in {1..5}; do
cmd
done
Heredoc
cat <<[term to end on in all CAPS]
[text text text]
[TERM]
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)
IFS
The shell treats each character of IFS (Internal Field Separator) as a delimiter, and splits the results of the other expansions into words on these characters. The default value of IFS is a space, a tab, and a newline.
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"