Added arithmetic operators

This commit is contained in:
Dylan Araps
2018-06-21 18:00:05 +10:00
parent b5a2ad5e0a
commit 955370b5ce
11 changed files with 621 additions and 497 deletions

View File

@@ -1,93 +1,51 @@
# INTERNAL VARIABLES
# OBSOLETE SYNTAX
## Get the location to the `bash` binary
## Shebang
Use `#!/usr/bin/env bash` instead of `#!/bin/bash`.
- The former searches the user's `PATH` to find the `bash` binary.
- The latter assumes it is always installed to `/bin/` which can cause issues.
```shell
"$BASH"
# Right:
#!/usr/bin/env bash
# Wrong:
#!/bin/bash
```
## Get the version of the current running `bash` process
## Command Substitution
Use `$()` instead of `` ` ` ``.
```shell
# As a string.
"$BASH_VERSION"
# Right.
var="$(command)"
# As an array.
"${BASH_VERSINFO[@]}"
# Wrong.
var=`command`
# $() can easily be nested whereas `` cannot.
var="$(command "$(command)")"
```
## Open the user's preferred text editor
## Function Declaration
Do not use the `function` keyword, it reduces compatibility with older versions of `bash`.
```shell
"$EDITOR" "$file"
# Right.
do_something() {
# ...
}
# NOTE: This variable may be empty, set a fallback value.
"${EDITOR:-vi}" "$file"
```
## Get the name of the current function
```shell
# Current function.
"${FUNCNAME[0]}"
# Parent function.
"${FUNCNAME[1]}"
# So on and so forth.
"${FUNCNAME[2]}"
"${FUNCNAME[3]}"
# All functions including parents.
"${FUNCNAME[@]}"
```
## Get the host-name of the system
```shell
"$HOSTNAME"
# NOTE: This variable may be empty.
# Optionally set a fallback to the hostname command.
"${HOSTNAME:-$(hostname)}"
```
## Get the architecture of the Operating System
```shell
"$HOSTTYPE"
```
## Get the name of the Operating System / Kernel
This can be used to add conditional support for different Operating
Systems without needing to call `uname`.
```shell
"$OSTYPE"
```
## Get the current working directory
This is an alternative to the `pwd` built-in.
```shell
"$PWD"
```
## Get the number of seconds the script has been running
```shell
"$SECONDS"
```
## Get a pseudorandom integer
Each time `$RANDOM` is used, a different integer between `0` and `32767` is returned. This variable should not be used for anything related to security (*this includes encryption keys etc*).
```shell
"$RANDOM"
# Wrong.
function do_something() {
# ...
}
```
<!-- CHAPTER END -->