Skip to content

Commit ab5a6dc

Browse files
E-Firagladysh
authored andcommitted
add bash-scripting-guidelines.md
1 parent d6dd3df commit ab5a6dc

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

bash-scripting-guidelines.md

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
Bash scripting guidelines
2+
=========================
3+
4+
## Interpreter
5+
6+
For better portability be sure to specify the interpreter in this way:
7+
```shell
8+
#! /usr/bin/env bash
9+
```
10+
11+
## Set options
12+
13+
For prevent hidden bugs use `set -euox pipefail` as the first command
14+
when possible.
15+
For additional info, please see
16+
[manpages](http://manpages.ubuntu.com/manpages/hirsute/en/man1/set.1posix.html)
17+
18+
## Variables
19+
20+
Variables local to the script must be in the lower case.
21+
22+
Using variable is allowed only wrapped in `"${}"`.
23+
Example:
24+
```shell
25+
var=123
26+
echo "${var}"
27+
```
28+
29+
If you need to use `"$@"`, always use quotation marks.
30+
Example:
31+
```shell
32+
lua test/test.lua "$@"
33+
```
34+
35+
If the script is sensitive to the current directory, store it in the `ROOT`
36+
variable. Example:
37+
```shell
38+
ROOT="${BASH_SOURCE[0]}"
39+
if([ -h "${ROOT}" ]) then
40+
while([ -h "${ROOT}" ]) do ROOT=`readlink "${ROOT}"`; done
41+
fi
42+
ROOT=$(cd `dirname "${ROOT}"` && pwd)
43+
```
44+
45+
## Wrapping other programs
46+
47+
If the script is a wrapper around another program and no postprocessing is
48+
needed after execution of that program, you should strive to replace the shell
49+
without creating additional processes, using `exec`. Execution of the wrapper
50+
script will be terminated after the `exec` call.
51+
Example:
52+
```shell
53+
exec /usr/bin/env "${LE_LUA_INTERPRETER}" "$@"
54+
```

0 commit comments

Comments
 (0)