update manuscript

This commit is contained in:
Dylan Araps 2018-06-20 16:00:33 +10:00
parent 393a288242
commit d2c42689d8
18 changed files with 43 additions and 52 deletions

View File

@ -1,10 +1,10 @@
# Introduction
# FOREWORD
A collection of pure `bash` alternatives to external processes and programs. The `bash` scripting language is more powerful than people realise and you can accomplish most tasks without the need or dependency of external programs.
A collection of pure `bash` alternatives to external processes and programs. The `bash` scripting language is more powerful than people realise and most tasks can be accomplished without the need for or dependence on external programs.
Calling an external process in `bash` is expensive and excessive use will cause a noticeable slowdown. By sticking to built-in methods (*where possible*) your scripts and programs will be faster, require less dependencies and you'll gain a better understanding of the language itself.
Calling an external process in `bash` is expensive and excessive use will cause a noticeable slowdown. Scripts and programs written using built-in methods (*where applicable*) will be faster, require less dependencies and afford a better understanding of the language itself.
The contents of this book provide a reference for solving the problems encountered when writing programs and scripts in `bash`. The examples are in function format showcasing how to incorporate these solutions into your code.
The content of this book provides a reference for solving problems encountered when writing programs and scripts in `bash`. Examples are in function format showcasing how to incorporate these solutions into code.
<!-- CHAPTER END -->

View File

@ -1,4 +1,4 @@
# Strings
# STRINGS
## Trim leading and trailing white-space from string
@ -353,7 +353,7 @@ if [[ "$var" == sub_string* ]]; then
printf '%s\n' "var starts with sub_string."
fi
# Inverse (var doesn't start with sub_string).
# Inverse (var does not start with sub_string).
if [[ "$var" != sub_string* ]]; then
printf '%s\n' "var does not start with sub_string."
fi
@ -366,7 +366,7 @@ if [[ "$var" == *sub_string ]]; then
printf '%s\n' "var ends with sub_string."
fi
# Inverse (var doesn't start with sub_string).
# Inverse (var does not start with sub_string).
if [[ "$var" != *sub_string ]]; then
printf '%s\n' "var does not end with sub_string."
fi

View File

@ -1,4 +1,4 @@
# Arithmetic
# ARITHMETIC
## Simpler syntax to set variables
@ -16,7 +16,7 @@
((var=var2*arr[2]))
```
## Ternary tests
## Ternary Tests
```shell
# Set the value of var to var2 if var2 is greater than var.

View File

@ -1,8 +1,8 @@
# Traps
# TRAPS
Traps allow you to execute code on various signals. In `pxltrm` I'm using traps to redraw the user interface on window resize. Another use case is cleaning up temporary files on script exit.
Traps allow a script to execute code on various signals. In [pxltrm](https://github.com/dylanaraps/pxltrm) (*a pixel art editor written in bash*) traps are used to redraw the user interface on window resize. Another use case is cleaning up temporary files on script exit.
These `trap` lines should be added near the start of your script so any early errors are also caught.
Traps should be added near the start of scripts so any early errors are also caught.
**NOTE:** For a full list of signals, see `trap -l`.

View File

@ -1,8 +1,8 @@
# Performance
# PERFORMANCE
## Disable Unicode
If your script doesn't require unicode, you can disable it for a speed boost. Results may vary but I've seen an improvement in Neofetch and some other smaller programs.
If unicode is not required, it can be disabled for a performance increase. Results may vary however there have been noticeable improvements in [neofetch](https://github.com/dylanaraps/neofetch) and other programs.
```shell
# Disable unicode.

View File

@ -1,4 +1,4 @@
# Obsolete Syntax
# OBSOLETE SYNTAX
## Shebang
@ -34,7 +34,7 @@ var="$(command "$(command)")"
## Function Declaration
Don't use the `function` keyword, it reduces compatibility with older versions of `bash`.
Do not use the `function` keyword, it reduces compatibility with older versions of `bash`.
```shell
# Right.

View File

@ -1,10 +1,4 @@
# Internal Variables
**NOTE**: This list does not include every internal variable (*You can
help by adding a missing entry!*).
For a complete list, see:
http://tldp.org/LDP/abs/html/internalvariables.html
# INTERNAL VARIABLES
## Get the location to the `bash` binary

View File

@ -1,4 +1,4 @@
# Information about the terminal
# INFORMATION ABOUT THE TERMINAL
## Get the terminal size in lines and columns (*from a script*)

View File

@ -1,4 +1,4 @@
# Conversion
# CONVERSION
## Convert a hex color to RGB
@ -42,7 +42,7 @@ $ rgb_to_hex "255" "255" "255"
```
# Code Golf
# CODE GOLF
## Shorter `for` loop syntax
@ -80,13 +80,13 @@ f(){ echo hi;}
f()(echo hi)
# Using arithmetic
# You can use this to assign integer values.
# This can be used to assign integer values.
# Example: f a=1
# f a++
f()(($1))
# Using tests, loops etc.
# NOTE: You can also use while, until, case, (()), [[]].
# NOTE: while, until, case, (()), [[]] can also be used.
f()if true; then echo "$1"; fi
f()for i in "$@"; do echo "$i"; done
```

View File

@ -1,9 +1,8 @@
# Other
# OTHER
## Use `read` as an alternative to the `sleep` command
I was surprised to find out `sleep` is an external command and isn't a
built-in.
Surprisingly, `sleep` is an external command and not a `bash` built-in.
**CAVEAT:** Requires `bash` 4+
@ -28,8 +27,7 @@ read_sleep 30
## Check if a program is in the user's PATH
```shell
# There are 3 ways to do this and you can use either of
# these in the same way.
# There are 3 ways to do this and either one can be used.
type -p executable_name &>/dev/null
hash executable_name &>/dev/null
command -v executable_name &>/dev/null
@ -44,9 +42,9 @@ if ! type -p executable_name &>/dev/null; then
# Program is not in PATH.
fi
# Example (Exit early if program isn't installed).
# Example (Exit early if program is not installed).
if ! type -p convert &>/dev/null; then
printf '%s\n' "error: convert isn't installed, exiting..."
printf '%s\n' "error: convert is not installed, exiting..."
exit 1
fi
```
@ -158,7 +156,7 @@ done
printf '\n'
```
## Get the list of functions from your script
## Get the list of functions in a script
```sh
get_functions() {

View File

@ -1,4 +1,4 @@
# Arrays
# ARRAYS
## Reverse an array
@ -92,7 +92,7 @@ $ array=(red green blue yellow brown)
$ random_array_element "${array[@]}"
yellow
# You can also just pass multiple arguments.
# Multiple arguments can also be passed.
$ random_array_element 1 2 3 4 5 6 7
3
```

View File

@ -1,8 +1,8 @@
# Loops
# LOOPS
## Loop over a range of numbers
Don't use `seq`.
Alternative to `seq`.
```shell
# Loop from 0-100 (no variable support).
@ -13,7 +13,7 @@ done
## Loop over a variable range of numbers
Don't use `seq`.
Alternative to `seq`.
```shell
# Loop from 0-VAR.

View File

@ -1,6 +1,6 @@
# File handling
# FILE HANDLING
**CAVEAT:** `bash` doesn't handle binary data properly in versions `< 4.4`.
**CAVEAT:** `bash` does not handle binary data properly in versions `< 4.4`.
## Read a file to a string
@ -92,8 +92,7 @@ lines() {
**Example Function (bash 3):**
This method uses less memory than the `mapfile` method and it's more
compatible but it's slower for bigger files.
This method uses less memory than the `mapfile` method and works in `bash` 3 but it is slower for bigger files.
```sh
lines_loop() {

View File

@ -1,4 +1,4 @@
# File Paths
# FILE PATHS
## Get the directory name of a file path

View File

@ -1,4 +1,4 @@
# Variables
# VARIABLES
## Assign and access a variable using a variable

View File

@ -1,6 +1,6 @@
# Escape Sequences
# ESCAPE SEQUENCES
Contrary to popular belief, there's no issue in using raw escape sequences. Using `tput` just abstracts the same ANSI escape sequences. What's worse is that `tput` isn't actually portable, there are a number of different `tput` variants on different Operating Systems each with different commands (*try and run `tput setaf 3` on a FreeBSD system*). The easiest solution ends up being raw ANSI sequences.
Contrary to popular belief, there is no issue in utilizing raw escape sequences. Using `tput` abstracts the same ANSI sequences as if printed manually. Worse still, `tput` is not actually portable. There are a number of `tput` variants each with different commands and syntaxes (*try `tput setaf 3` on a FreeBSD system*). Raw sequences are fine.
## Text Colors

View File

@ -1,4 +1,4 @@
# Parameter Expansion
# PARAMETER EXPANSION
## Indirection
@ -58,7 +58,7 @@
| `${VAR-STRING}` | If `VAR` is unset, use `STRING` as it's value.
| `${VAR:=STRING}` | If `VAR` is empty or unset, set the value of `VAR` to `STRING`.
| `${VAR=STRING}` | If `VAR` is unset, set the value of `VAR` to `STRING`.
| `${VAR:+STRING}` | If `VAR` isn't empty, use `STRING` as it's value.
| `${VAR:+STRING}` | If `VAR` is not empty, use `STRING` as it's value.
| `${VAR+STRING}` | If `VAR` is set, use `STRING` as it's value.
| `${VAR:?STRING}` | Display an error if empty or unset.
| `${VAR?STRING}` | Display an error if unset.

View File

@ -1,4 +1,4 @@
# Brace Expansion
# BRACE EXPANSION
## Ranges