docs: update

This commit is contained in:
Dylan Araps 2018-06-14 08:51:08 +10:00
parent ac40a6a396
commit 25744eeeac
1 changed files with 42 additions and 2 deletions

View File

@ -63,7 +63,8 @@ scripts and not full blown utilities.
* [Shorter `for` loop syntax.](#shorter-for-loop-syntax)
* [Shorter infinite loops.](#shorter-infinite-loops)
* [Shorter function declaration.](#shorter-function-declaration)
* [Shorter if syntax.](#shorter-if-syntax)
* [Shorter `if` syntax.](#shorter-if-syntax)
* [Simpler `case` statement to set variable.](#simpler-case-statement-to-set-variable)
* [Miscellaneous](#miscellaneous)
* [Get the current date using `strftime`.](#get-the-current-date-using-strftime)
* [Bypass shell aliases and functions.](#bypass-shell-aliases-and-functions)
@ -496,7 +497,7 @@ f()if true; then echo "$1"; fi
f()for i in "$@"; do echo "$i"; done
```
### Shorter if syntax.
### Shorter `if` syntax.
```sh
# One line
@ -514,6 +515,45 @@ f()for i in "$@"; do echo "$i"; done
}
```
### Simpler `case` statement to set variable.
We can use the `:` builtin to avoid repeating `variable=` in a case
statement. The `$_` variable stores the last argument of the last
successful command. `:` always succeeds so we can abuse it to store the
variable value.
```sh
# Example snippet from Neofetch.
case "$(uname)" in
"SunOS"): "Solaris" ;;
"Haiku"): "Haiku" ;;
"MINIX"): "MINIX" ;;
"AIX"): "AIX" ;;
"IRIX"*): "IRIX" ;;
"FreeMiNT"): "FreeMiNT" ;;
"Linux" | "GNU"*)
: "Linux"
;;
*"BSD" | "DragonFly" | "Bitrig")
: "BSD"
;;
"CYGWIN"* | "MSYS"* | "MINGW"*)
: "Windows"
;;
*)
printf '%s\n' "Unknown OS detected: '$kernel_name', aborting..." >&2
printf '%s\n' "Open an issue on GitHub to add support for your OS." >&2
exit 1
;;
esac
printf '%s\n' "$_"
```
## Miscellaneous
### Get the current date using `strftime`.