Make the snippets sourceable.

This commit is contained in:
Dylan Araps
2018-06-14 18:46:28 +10:00
parent 09ae875000
commit 8140a47dfb

View File

@@ -126,12 +126,14 @@ trim() {
### Split a string on a delimiter. ### Split a string on a delimiter.
```sh ```sh
_() {
# To multiple variables. # To multiple variables.
string="1,2,3" string="1,2,3"
IFS=, read -r var1 var2 var3 <<< "$string" IFS=, read -r var1 var2 var3 <<< "$string"
# To an array. # To an array.
IFS=, read -ra vars <<< "$string" IFS=, read -ra vars <<< "$string"
}
``` ```
### Change a string to lowercase. ### Change a string to lowercase.
@@ -189,8 +191,8 @@ rstrip() {
### Assign and access a variable using a variable. ### Assign and access a variable using a variable.
```sh ```sh
_() {
# First Example. # First Example.
var1="world" var1="world"
var2="hello_${var1}" var2="hello_${var1}"
declare "${var2}=test_string" declare "${var2}=test_string"
@@ -198,7 +200,6 @@ printf '%s\n' "${!var2}"
# Second Example. # Second Example.
# Assign to a variable named after the # Assign to a variable named after the
# value stored in '$var'. # value stored in '$var'.
var="test" var="test"
@@ -209,6 +210,7 @@ printf '%s\n' "set var \$$var to '${!var}'"
# Access the variable directly. # Access the variable directly.
printf '%s\n' "set var \$$var to '$test'" printf '%s\n' "set var \$$var to '$test'"
}
``` ```
@@ -290,7 +292,9 @@ cycle() {
Alternative to the `cat` command. Alternative to the `cat` command.
```sh ```sh
_() {
file_data="$(<"file")" file_data="$(<"file")"
}
``` ```
### Read a file to an array (*by line*). ### Read a file to an array (*by line*).
@@ -298,11 +302,13 @@ file_data="$(<"file")"
Alternative to the `cat` command. Alternative to the `cat` command.
```sh ```sh
_() {
# Bash <4 # Bash <4
IFS=$'\n' read -d "" -ra file_data < "file" IFS=$'\n' read -d "" -ra file_data < "file"
# Bash 4+ # Bash 4+
mapfile -t file_data < "file" mapfile -t file_data < "file"
}
``` ```
### Get the first N lines of a file. ### Get the first N lines of a file.
@@ -352,6 +358,7 @@ lines() {
Dont use `ls`. Dont use `ls`.
```sh ```sh
_() {
# Greedy example. # Greedy example.
for file in *; do for file in *; do
printf '%s\n' "$file" printf '%s\n' "$file"
@@ -373,6 +380,7 @@ for file in ~/Pictures/**/*; do
printf '%s\n' "$file" printf '%s\n' "$file"
done done
shopt -u globstar shopt -u globstar
}
``` ```
### Count files or directories in directory. ### Count files or directories in directory.
@@ -393,11 +401,14 @@ count() {
Alternative to `touch`. Alternative to `touch`.
```sh ```sh
_() {
# Shortest.
:> file :> file
# Longer alternatives: # Longer alternatives:
echo -n > file echo -n > file
printf '' > file printf '' > file
}
``` ```
## File Paths ## File Paths
@@ -430,6 +441,7 @@ basename() {
### Simpler syntax to set variables. ### Simpler syntax to set variables.
```sh ```sh
_() {
# Simple math # Simple math
((var=1+2)) ((var=1+2))
@@ -441,17 +453,20 @@ basename() {
# Using variables # Using variables
((var=var2*arr[2])) ((var=var2*arr[2]))
}
``` ```
### Ternary tests. ### Ternary tests.
```sh ```sh
_() {
# Set the value of var to var2 if var2 is greater than var. # Set the value of var to var2 if var2 is greater than var.
# var: variable to set. # var: variable to set.
# var2>var: Condition to test. # var2>var: Condition to test.
# ?var2: If the test succeeds. # ?var2: If the test succeeds.
# :var: If the test fails. # :var: If the test fails.
((var=var2>var?var2:var)) ((var=var2>var?var2:var))
}
``` ```
## Colors ## Colors
@@ -526,33 +541,38 @@ get_cursor_pos() {
### Shorter `for` loop syntax. ### Shorter `for` loop syntax.
```sh ```sh
_() {
# Tiny C Style. # Tiny C Style.
for((;i++<10;)){ echo "$i";} for((;i++<10;)){ echo "$i";}
# Undocumented method. # Undocumented method.
# Note: This is commented to make shellcheck play nice. # Note: This is commented to make shellcheck play nice.
# for i in {1..10};{ echo "$i";} for i in {1..10};{ echo "$i";}
# Expansion. # Expansion.
for i in {1..10}; do echo "$i"; done for i in {1..10}; do echo "$i"; done
# C Style. # C Style.
for((i=0;i<=10;i++)); do echo "$i"; done for((i=0;i<=10;i++)); do echo "$i"; done
}
``` ```
### Shorter infinite loops. ### Shorter infinite loops.
```sh ```sh
_() {
# Normal method # Normal method
while :; do code; done while :; do echo hi; done
# Shorter # Shorter
for((;;)){ code;} for((;;)){ echo hi;}
}
``` ```
### Shorter function declaration. ### Shorter function declaration.
```sh ```sh
_() {
# Normal method # Normal method
f(){ echo hi;} f(){ echo hi;}
@@ -567,14 +587,15 @@ f()(($1))
# Using tests, loops etc. # Using tests, loops etc.
# Note: You can also use while, until, case, (()), [[]]. # Note: You can also use while, until, case, (()), [[]].
# Note: These are commented to make shellcheck play nice. f()if true; then echo "$1"; fi
# f()if true; then echo "$1"; fi f()for i in "$@"; do echo "$i"; done
# f()for i in "$@"; do echo "$i"; done }
``` ```
### Shorter `if` syntax. ### Shorter `if` syntax.
```sh ```sh
_() {
# One line # One line
[[ "$var" == hello ]] && echo hi || echo bye [[ "$var" == hello ]] && echo hi || echo bye
[[ "$var" == hello ]] && { echo hi; echo there; } || echo bye [[ "$var" == hello ]] && { echo hi; echo there; } || echo bye
@@ -588,6 +609,7 @@ f()(($1))
echo hi echo hi
# ... # ...
} }
}
``` ```
### Simpler `case` statement to set variable. ### Simpler `case` statement to set variable.
@@ -598,15 +620,9 @@ successful command. `:` always succeeds so we can abuse it to store the
variable value. variable value.
```sh ```sh
_() {
# Example snippet from Neofetch. # Example snippet from Neofetch.
case "$(uname)" in case "$(uname)" in
"SunOS"): "Solaris" ;;
"Haiku"): "Haiku" ;;
"MINIX"): "MINIX" ;;
"AIX"): "AIX" ;;
"IRIX"*): "IRIX" ;;
"FreeMiNT"): "FreeMiNT" ;;
"Linux" | "GNU"*) "Linux" | "GNU"*)
: "Linux" : "Linux"
;; ;;
@@ -620,14 +636,14 @@ case "$(uname)" in
;; ;;
*) *)
printf '%s\n' "Unknown OS detected: '$kernel_name', aborting..." >&2 printf '%s\n' "Unknown OS detected, aborting..." >&2
printf '%s\n' "Open an issue on GitHub to add support for your OS." >&2
exit 1 exit 1
;; ;;
esac esac
# Finally, set the variable. # Finally, set the variable.
os="$_" os="$_"
}
``` ```
## Internal Variables ## Internal Variables
@@ -641,59 +657,59 @@ http://tldp.org/LDP/abs/html/internalvariables.html
### Get the location to the `bash` binary. ### Get the location to the `bash` binary.
```sh ```sh
"$BASH" : "$BASH"
``` ```
### Get the version of the current running `bash` process. ### Get the version of the current running `bash` process.
```sh ```sh
# As a string. # As a string.
"$BASH_VERSION" : "$BASH_VERSION"
# As an array. # As an array.
"${BASH_VERSINFO[@]}" : "${BASH_VERSINFO[@]}"
``` ```
### Open the user's preferred text editor. ### Open the user's preferred text editor.
```sh ```sh
"$EDITOR" "$file" : "$EDITOR" "$file"
# NOTE: This variable may be empty, set a fallback value. # NOTE: This variable may be empty, set a fallback value.
"${EDITOR:-vi}" "$file" : "${EDITOR:-vi}" "$file"
``` ```
### Get the name of the current function. ### Get the name of the current function.
```sh ```sh
# Current function. # Current function.
"${FUNCNAME[0]}" : "${FUNCNAME[0]}"
# Parent function. # Parent function.
"${FUNCNAME[1]}" : "${FUNCNAME[1]}"
# So on and so forth. # So on and so forth.
"${FUNCNAME[2]}" : "${FUNCNAME[2]}"
"${FUNCNAME[3]}" : "${FUNCNAME[3]}"
# All functions including parents. # All functions including parents.
"${FUNCNAME[@]}" : "${FUNCNAME[@]}"
``` ```
### Get the host-name of the system. ### Get the host-name of the system.
```sh ```sh
"$HOSTNAME" : "$HOSTNAME"
# NOTE: This variable may be empty. # NOTE: This variable may be empty.
# Optionally set a fallback to the hostname command. # Optionally set a fallback to the hostname command.
"${HOSTNAME:-$(hostname)}" : "${HOSTNAME:-$(hostname)}"
``` ```
### Get the architecture of the Operating System. ### Get the architecture of the Operating System.
```sh ```sh
"$HOSTTYPE" : "$HOSTTYPE"
``` ```
### Get the name of the Operating System / Kernel. ### Get the name of the Operating System / Kernel.
@@ -702,7 +718,7 @@ This can be used to add conditional support for different Operating
Systems without needing to call `uname`. Systems without needing to call `uname`.
```sh ```sh
"$OSTYPE" : "$OSTYPE"
``` ```
### Get the current working directory. ### Get the current working directory.
@@ -710,13 +726,13 @@ Systems without needing to call `uname`.
This is an alternative to the `pwd` built-in. This is an alternative to the `pwd` built-in.
```sh ```sh
"$PWD" : "$PWD"
``` ```
### Get the number of seconds the script has been running. ### Get the number of seconds the script has been running.
```sh ```sh
"$SECONDS" : "$SECONDS"
``` ```
## Other ## Other
@@ -738,33 +754,33 @@ date() {
# Examples: # Examples:
# Using date. # Using date.
date "+%a %d %b - %l:%M %p" : date "+%a %d %b - %l:%M %p"
# Using printf. # Using printf.
printf '%(%a %d %b - %l:%M %p)T\n' '-1' : printf '%(%a %d %b - %l:%M %p)T\n' '-1'
# Assigning a variable. # Assigning a variable.
printf -v date '%(%a %d %b - %l:%M %p)T\n' '-1' : printf -v date '%(%a %d %b - %l:%M %p)T\n' '-1'
``` ```
### Bypass shell aliases. ### Bypass shell aliases.
```sh ```sh
# alias # alias
ls : ls
# command # command
# shellcheck disable=SC1001 # shellcheck disable=SC1001
\ls : \ls
``` ```
### Bypass shell functions. ### Bypass shell functions.
```sh ```sh
# function # function
ls : ls
# command # command
command ls : command ls
``` ```