pure-bash-bible/manuscript/chapter15.txt

56 lines
871 B
Plaintext
Raw Permalink Normal View History

2018-06-23 02:34:15 +02:00
# OBSOLETE SYNTAX
2018-06-23 02:34:15 +02:00
## Shebang
2018-06-20 05:03:53 +02:00
2018-06-23 02:34:15 +02:00
Use `#!/usr/bin/env bash` instead of `#!/bin/bash`.
2018-06-23 02:34:15 +02:00
- 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.
2019-09-20 07:53:15 +02:00
**NOTE**: There are times when one may have a good reason for using `#!/bin/bash` or another direct path to the binary.
```shell
2018-06-23 02:34:15 +02:00
# Right:
2018-06-21 10:00:05 +02:00
2018-06-23 02:34:15 +02:00
#!/usr/bin/env bash
2019-09-20 07:53:15 +02:00
# Less right:
2018-06-23 02:34:15 +02:00
#!/bin/bash
```
2018-06-23 02:34:15 +02:00
## Command Substitution
2018-06-23 02:34:15 +02:00
Use `$()` instead of `` ` ` ``.
2018-06-21 10:00:05 +02:00
```shell
2018-06-23 02:34:15 +02:00
# Right.
var="$(command)"
2018-06-23 02:34:15 +02:00
# Wrong.
var=`command`
2018-06-23 02:34:15 +02:00
# $() can easily be nested whereas `` cannot.
var="$(command "$(command)")"
```
2018-06-23 02:34:15 +02:00
## Function Declaration
2018-06-21 10:00:05 +02:00
2018-06-23 02:34:15 +02:00
Do not use the `function` keyword, it reduces compatibility with older versions of `bash`.
2018-06-21 10:00:05 +02:00
```shell
2018-06-23 02:34:15 +02:00
# Right.
do_something() {
# ...
}
2018-06-21 10:00:05 +02:00
2018-06-23 02:34:15 +02:00
# Wrong.
function do_something() {
# ...
}
```
<!-- CHAPTER END -->