mirror of
https://github.com/dylanaraps/pure-bash-bible.git
synced 2025-01-17 04:08:17 +01:00
56 lines
871 B
Plaintext
56 lines
871 B
Plaintext
# OBSOLETE SYNTAX
|
|
|
|
## 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.
|
|
|
|
**NOTE**: There are times when one may have a good reason for using `#!/bin/bash` or another direct path to the binary.
|
|
|
|
|
|
```shell
|
|
# Right:
|
|
|
|
#!/usr/bin/env bash
|
|
|
|
# Less right:
|
|
|
|
#!/bin/bash
|
|
```
|
|
|
|
## Command Substitution
|
|
|
|
Use `$()` instead of `` ` ` ``.
|
|
|
|
```shell
|
|
# Right.
|
|
var="$(command)"
|
|
|
|
# Wrong.
|
|
var=`command`
|
|
|
|
# $() can easily be nested whereas `` cannot.
|
|
var="$(command "$(command)")"
|
|
```
|
|
|
|
## Function Declaration
|
|
|
|
Do not use the `function` keyword, it reduces compatibility with older versions of `bash`.
|
|
|
|
```shell
|
|
# Right.
|
|
do_something() {
|
|
# ...
|
|
}
|
|
|
|
# Wrong.
|
|
function do_something() {
|
|
# ...
|
|
}
|
|
```
|
|
|
|
<!-- CHAPTER END -->
|
|
|