Make the snippets sourceable.

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

314
README.md
View File

@@ -126,12 +126,14 @@ trim() {
### Split a string on a delimiter. ### Split a string on a delimiter.
```sh ```sh
# To multiple variables. _() {
string="1,2,3" # To multiple variables.
IFS=, read -r var1 var2 var3 <<< "$string" string="1,2,3"
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,26 +191,26 @@ 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"
printf '%s\n' "${!var2}" printf '%s\n' "${!var2}"
# Second Example. # Second Example.
# Assign to a variable named after the
# value stored in '$var'.
var="test"
read -rp "input text: " "${var?}"
# Assign to a variable named after the # Access the variable indirectly.
# value stored in '$var'. printf '%s\n' "set var \$$var to '${!var}'"
var="test"
read -rp "input text: " "${var?}"
# Access the variable indirectly. # Access the variable directly.
printf '%s\n' "set var \$$var to '${!var}'" printf '%s\n' "set var \$$var to '$test'"
}
# Access the variable directly.
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 _() {
IFS=$'\n' read -d "" -ra file_data < "file" # Bash <4
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,27 +358,29 @@ lines() {
Dont use `ls`. Dont use `ls`.
```sh ```sh
# Greedy example. _() {
for file in *; do # Greedy example.
printf '%s\n' "$file" for file in *; do
done printf '%s\n' "$file"
done
# PNG files in dir. # PNG files in dir.
for file in ~/Pictures/*.png; do for file in ~/Pictures/*.png; do
printf '%s\n' "$file" printf '%s\n' "$file"
done done
# Iterate over directories. # Iterate over directories.
for dir in ~/Downloads/*/; do for dir in ~/Downloads/*/; do
printf '%s\n' "$dir" printf '%s\n' "$dir"
done done
# Iterate recursively. # Iterate recursively.
shopt -s globstar shopt -s globstar
for file in ~/Pictures/**/*; do 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
:> file _() {
# Shortest.
:> file
# Longer alternatives: # Longer alternatives:
echo -n > file echo -n > file
printf '' > file printf '' > file
}
``` ```
## File Paths ## File Paths
@@ -430,28 +441,32 @@ basename() {
### Simpler syntax to set variables. ### Simpler syntax to set variables.
```sh ```sh
# Simple math _() {
((var=1+2)) # Simple math
((var=1+2))
# Decrement/Increment variable # Decrement/Increment variable
((var++)) ((var++))
((var--)) ((var--))
((var+=1)) ((var+=1))
((var-=1)) ((var-=1))
# 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. _() {
# var: variable to set. # Set the value of var to var2 if var2 is greater than var.
# var2>var: Condition to test. # var: variable to set.
# ?var2: If the test succeeds. # var2>var: Condition to test.
# :var: If the test fails. # ?var2: If the test succeeds.
((var=var2>var?var2:var)) # :var: If the test fails.
((var=var2>var?var2:var))
}
``` ```
## Colors ## Colors
@@ -526,67 +541,74 @@ get_cursor_pos() {
### Shorter `for` loop syntax. ### Shorter `for` loop syntax.
```sh ```sh
# Tiny C Style. _() {
for((;i++<10;)){ echo "$i";} # Tiny C Style.
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 _() {
while :; do code; done # Normal method
while :; do echo hi; done
# Shorter # Shorter
for((;;)){ code;} for((;;)){ echo hi;}
}
``` ```
### Shorter function declaration. ### Shorter function declaration.
```sh ```sh
# Normal method _() {
f(){ echo hi;} # Normal method
f(){ echo hi;}
# Using a subshell # Using a subshell
f()(echo hi) f()(echo hi)
# Using arithmetic # Using arithmetic
# You can use this to assign integer values. # You can use this to assign integer values.
# Example: f a=1 # Example: f a=1
# f a++ # f a++
f()(($1)) 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 _() {
[[ "$var" == hello ]] && echo hi || echo bye # One line
[[ "$var" == hello ]] && { echo hi; echo there; } || echo bye [[ "$var" == hello ]] && echo hi || echo bye
[[ "$var" == hello ]] && { echo hi; echo there; } || echo bye
# Multi line (no else, single statement) # Multi line (no else, single statement)
[[ "$var" == hello ]] && \ [[ "$var" == hello ]] && \
echo hi echo hi
# Multi line (no else) # Multi line (no else)
[[ "$var" == hello ]] && { [[ "$var" == hello ]] && {
echo hi echo hi
# ... # ...
}
} }
``` ```
@@ -598,36 +620,30 @@ successful command. `:` always succeeds so we can abuse it to store the
variable value. variable value.
```sh ```sh
# Example snippet from Neofetch. _() {
case "$(uname)" in # Example snippet from Neofetch.
"SunOS"): "Solaris" ;; case "$(uname)" in
"Haiku"): "Haiku" ;; "Linux" | "GNU"*)
"MINIX"): "MINIX" ;; : "Linux"
"AIX"): "AIX" ;; ;;
"IRIX"*): "IRIX" ;;
"FreeMiNT"): "FreeMiNT" ;;
"Linux" | "GNU"*) *"BSD" | "DragonFly" | "Bitrig")
: "Linux" : "BSD"
;; ;;
*"BSD" | "DragonFly" | "Bitrig") "CYGWIN"* | "MSYS"* | "MINGW"*)
: "BSD" : "Windows"
;; ;;
"CYGWIN"* | "MSYS"* | "MINGW"*) *)
: "Windows" printf '%s\n' "Unknown OS detected, aborting..." >&2
;; exit 1
;;
esac
*) # Finally, set the variable.
printf '%s\n' "Unknown OS detected: '$kernel_name', aborting..." >&2 os="$_"
printf '%s\n' "Open an issue on GitHub to add support for your OS." >&2 }
exit 1
;;
esac
# Finally, set the variable.
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
``` ```