Skip to content

Instantly share code, notes, and snippets.

@johnbuhay
Last active January 17, 2017 18:49
Show Gist options
  • Select an option

  • Save johnbuhay/d5d205802019fb3b646083f7a0cfe731 to your computer and use it in GitHub Desktop.

Select an option

Save johnbuhay/d5d205802019fb3b646083f7a0cfe731 to your computer and use it in GitHub Desktop.
Cheatsheet
---
Resources:
- https://google.github.io/styleguide/shell.xml
- http://tldp.org/guides.html
#!/bin/bash
# 3.2.5. Special parameters
# http://tldp.org/LDP/Bash-Beginners-Guide/html/Bash-Beginners-Guide.html#sect_03_02_05
Character | Definition
$* | DEPRECATED: Expands to the positional parameters, starting from one. When the expansion occurs within double quotes, it expands to a single word with the value of each parameter separated by the first character of the IFS special variable.
$@ | Expands to the positional parameters, starting from one. When the expansion occurs within double quotes, each parameter expands to a separate word.
$# | Expands to the number of positional parameters in decimal.
$? | Expands to the exit status of the most recently executed foreground pipeline.
$- | A hyphen expands to the current option flags as specified upon invocation, by the set built-in command, or those set by the shell itself (such as the -i).
$$ | Expands to the process ID of the shell.
$! | Expands to the process ID of the most recently executed background (asynchronous) command.
$0 | Expands to the name of the shell or shell script.
$_ | The underscore variable is set at shell startup and contains the absolute file name of the shell or script being executed as passed in the argument list. Subsequently, it expands to the last argument to the previous command, after expansion. It is also set to the full pathname of each command executed and placed in the environment exported to that command. When checking mail, this parameter holds the name of the mail file.
#!/bin/bash
# 7.3. Using case statements
# http://tldp.org/LDP/Bash-Beginners-Guide/html/Bash-Beginners-Guide.html#sect_07_03
case "$1" in
1)
echo 1
;;
2 | 3)
echo $1 is 2 or 3
;;
[nN] | [yY] | No | Yes)$
echo $1 captured$
;;
*)
echo input captured by default section
;;
esac
#!/bin/bash
# 3.2. Variables
# http://tldp.org/LDP/Bash-Beginners-Guide/html/Bash-Beginners-Guide.html#sect_03_02
VARIABLE='value of variable'
VARIABLE=${VARIABLE:-default-value}
# 3.2.4. Reserved variables
# http://tldp.org/LDP/Bash-Beginners-Guide/html/Bash-Beginners-Guide.html#sect_03_02_04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment