added more examples

This commit is contained in:
Dylan Araps
2018-06-15 09:37:35 +10:00
parent 9f43812680
commit 7ab77b6024

122
README.md
View File

@@ -522,6 +522,8 @@ Alternative to the `head` command.
**NOTE:** Requires `bash` 4+ **NOTE:** Requires `bash` 4+
**Example Function:**
```sh ```sh
head() { head() {
# Usage: head "n" "file" # Usage: head "n" "file"
@@ -530,12 +532,25 @@ head() {
} }
``` ```
**Example Usage:**
```shell
$ head 2 ~/.bashrc
# Prompt
PS1='➜ '
$ head 1 ~/.bashrc
# Prompt
```
### Get the last N lines of a file. ### Get the last N lines of a file.
Alternative to the `tail` command. Alternative to the `tail` command.
**NOTE:** Requires `bash` 4+ **NOTE:** Requires `bash` 4+
**Example Function:**
```sh ```sh
tail() { tail() {
# Usage: tail "n" "file" # Usage: tail "n" "file"
@@ -550,6 +565,8 @@ Alternative to `wc -l`.
**NOTE:** Requires `bash` 4+ **NOTE:** Requires `bash` 4+
**Example Function:**
```sh ```sh
lines() { lines() {
# Usage lines "file" # Usage lines "file"
@@ -593,6 +610,8 @@ _() {
This works by passing the output of the glob as function arguments. We This works by passing the output of the glob as function arguments. We
then count the arguments and print the number. then count the arguments and print the number.
**Example Function:**
```sh ```sh
count() { count() {
# Usage: count /path/to/dir/* # Usage: count /path/to/dir/*
@@ -622,6 +641,8 @@ _() {
Alternative to the `dirname` command. Alternative to the `dirname` command.
**Example Function:**
```sh ```sh
dirname() { dirname() {
# Usage: dirname "path" # Usage: dirname "path"
@@ -633,6 +654,8 @@ dirname() {
Alternative to the `basename` command. Alternative to the `basename` command.
**Example Function:**
```sh ```sh
basename() { basename() {
# Usage: basename "path" # Usage: basename "path"
@@ -678,6 +701,8 @@ _() {
### Convert a hex color to RGB. ### Convert a hex color to RGB.
**Example Function:**
```sh ```sh
hex_to_rgb() { hex_to_rgb() {
# Usage: hex_to_rgb "#FFFFFF" # Usage: hex_to_rgb "#FFFFFF"
@@ -691,6 +716,8 @@ hex_to_rgb() {
### Convert an RGB color to hex. ### Convert an RGB color to hex.
**Example Function:**
```sh ```sh
rgb_to_hex() { rgb_to_hex() {
# Usage: rgb_to_hex "r" "g" "b" # Usage: rgb_to_hex "r" "g" "b"
@@ -705,6 +732,8 @@ rgb_to_hex() {
This is handy when writing scripts in pure bash and `stty`/`tput` cant be This is handy when writing scripts in pure bash and `stty`/`tput` cant be
called. called.
**Example Function:**
```sh ```sh
get_term_size() { get_term_size() {
# Usage: get_term_size # Usage: get_term_size
@@ -720,6 +749,8 @@ get_term_size() {
**NOTE**: This does not work in some terminal emulators. **NOTE**: This does not work in some terminal emulators.
**Example Function:**
```sh ```sh
get_window_size() { get_window_size() {
# Usage: get_window_size # Usage: get_window_size
@@ -733,6 +764,8 @@ get_window_size() {
This is useful when creating a TUI in pure bash. This is useful when creating a TUI in pure bash.
**Example Function:**
```sh ```sh
get_cursor_pos() { get_cursor_pos() {
# Usage: get_cursor_pos # Usage: get_cursor_pos
@@ -745,8 +778,7 @@ get_cursor_pos() {
### Shorter `for` loop syntax. ### Shorter `for` loop syntax.
```sh ```shell
_() {
# Tiny C Style. # Tiny C Style.
for((;i++<10;)){ echo "$i";} for((;i++<10;)){ echo "$i";}
@@ -759,25 +791,21 @@ _() {
# 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 ```shell
_() {
# Normal method # Normal method
while :; do echo hi; done while :; do echo hi; done
# Shorter # Shorter
for((;;)){ echo hi;} for((;;)){ echo hi;}
}
``` ```
### Shorter function declaration. ### Shorter function declaration.
```sh ```shell
_() {
# Normal method # Normal method
f(){ echo hi;} f(){ echo hi;}
@@ -792,16 +820,13 @@ _() {
# 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 ```shell
_() {
# 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
@@ -815,7 +840,6 @@ _() {
echo hi echo hi
# ... # ...
} }
}
``` ```
### Simpler `case` statement to set variable. ### Simpler `case` statement to set variable.
@@ -825,8 +849,7 @@ statement. The `$_` variable stores the last argument of the last
successful command. `:` always succeeds so we can abuse it to store the successful command. `:` always succeeds so we can abuse it to store the
variable value. variable value.
```sh ```shell
_() {
# Example snippet from Neofetch. # Example snippet from Neofetch.
case "$(uname)" in case "$(uname)" in
"Linux" | "GNU"*) "Linux" | "GNU"*)
@@ -849,7 +872,6 @@ _() {
# Finally, set the variable. # Finally, set the variable.
os="$_" os="$_"
}
``` ```
## Internal Variables ## Internal Variables
@@ -862,60 +884,60 @@ http://tldp.org/LDP/abs/html/internalvariables.html
### Get the location to the `bash` binary. ### Get the location to the `bash` binary.
```sh ```shell
: "$BASH" "$BASH"
``` ```
### Get the version of the current running `bash` process. ### Get the version of the current running `bash` process.
```sh ```shell
# 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 ```shell
: "$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 ```shell
# 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 ```shell
: "$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 ```shell
: "$HOSTTYPE" "$HOSTTYPE"
``` ```
### Get the name of the Operating System / Kernel. ### Get the name of the Operating System / Kernel.
@@ -923,22 +945,22 @@ http://tldp.org/LDP/abs/html/internalvariables.html
This can be used to add conditional support for different Operating This can be used to add conditional support for different Operating
Systems without needing to call `uname`. Systems without needing to call `uname`.
```sh ```shell
: "$OSTYPE" "$OSTYPE"
``` ```
### Get the current working directory. ### Get the current working directory.
This is an alternative to the `pwd` built-in. This is an alternative to the `pwd` built-in.
```sh ```shell
: "$PWD" "$PWD"
``` ```
### Get the number of seconds the script has been running. ### Get the number of seconds the script has been running.
```sh ```shell
: "$SECONDS" "$SECONDS"
``` ```
## Other ## Other
@@ -950,6 +972,8 @@ in place of the `date` command in a lot of cases.
**NOTE:** Requires `bash` 4+ **NOTE:** Requires `bash` 4+
**Example Function:**
```sh ```sh
date() { date() {
# Usage: date "format" # Usage: date "format"
@@ -971,22 +995,22 @@ date() {
### Bypass shell aliases. ### Bypass shell aliases.
```sh ```shell
# alias # alias
: ls ls
# command # command
# shellcheck disable=SC1001 # shellcheck disable=SC1001
: \ls \ls
``` ```
### Bypass shell functions. ### Bypass shell functions.
```sh ```shell
# function # function
: ls ls
# command # command
: command ls command ls
``` ```