You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+67-71Lines changed: 67 additions & 71 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -35,15 +35,15 @@ It's preferred that columns are 88 characters or less. The only reason that the
35
35
Do not use semicolons where they are not needed.
36
36
37
37
```bash
38
-
## Wrong
38
+
## Wrong.
39
39
name="dave";
40
40
echo"hello $name";
41
41
42
-
## Right
42
+
## Right.
43
43
name="dave"
44
44
echo"hello $name"
45
45
46
-
# Also right
46
+
# Also right.
47
47
name="dave";echo"hello $name"
48
48
```
49
49
@@ -56,12 +56,12 @@ Don't use the `function` keyword. Instead, `()` should be appended to the end of
56
56
```bash
57
57
## Wrong, if $i isn't used outside of 'foo'.
58
58
functionfoo {
59
-
i=foo # This is now global, wrong depending on intent
59
+
i=foo # This is now global, wrong depending on intent.
60
60
}
61
61
62
-
## Right
62
+
## Right.
63
63
foo() {
64
-
local i=foo # This is local, preferred
64
+
local i=foo # This is local, preferred.
65
65
}
66
66
```
67
67
@@ -70,36 +70,36 @@ foo() {
70
70
`then` should be on the same line as `if`, and `do` should be on the same line as `while`.
71
71
72
72
```bash
73
-
## Wrong
73
+
## Wrong.
74
74
iftrue
75
75
then
76
76
...
77
77
fi
78
78
79
-
## Also wrong
79
+
## Also wrong.
80
80
true&& {
81
81
...
82
82
}
83
83
84
-
## Right
84
+
## Right.
85
85
iftrue;then
86
86
...
87
87
fi
88
88
```
89
89
90
90
### Single line block statements
91
91
92
-
Block statements, such as `if`, `for`, and `while` loops, can be placed on a single line, as long as they fit within the preferred character limit, and don't negatively affect readability. It is required, though, that the end of the block statement, like `fi` and `done`, must be placed on a separate line.
92
+
Block statements, such as `if`, `for`, and `while` loops, can be placed on a single line, as long as they fit within the preferred character limit, and don't negatively affect readability. It is required, though, that the end of the block statement, like `fi` and `done`, are placed on a separate line.
93
93
94
94
```bash
95
-
## Wrong
95
+
## Wrong.
96
96
if [[ -f$file_name ]];thenecho"$file_name exists!";fi
97
97
98
-
## Also wrong
98
+
## Also wrong.
99
99
if [[ -f$file_name ]];thenecho"$file_name exists!";echo"Done"
100
100
fi
101
101
102
-
## Right
102
+
## Right.
103
103
if [[ -f$file_name ]];thenecho"$file_name exists!"
104
104
fi
105
105
```
@@ -137,13 +137,13 @@ func() {
137
137
}
138
138
```
139
139
140
-
#### Number of pound signs
140
+
#### Number of pound signsNumber of pound signs
141
141
142
-
When a comment refers to a single line of code, a singular pound sign (#) should be used. This includes when describing what the if statement checks for.
142
+
When a comment refers to a single line of code, a singular pound sign (`#`) should be used. This includes when describing what the if statement checks for.
@@ -153,7 +153,7 @@ if [[ ...code to be commented on... ]]; then
153
153
fi
154
154
```
155
155
156
-
When describing, say, what the code inside of an if statement does, always begin the comment with two pound signs instead of one. Two # indicate that the comment refers to a block of code instead of a single line/command. This is also applicable when making a single comment that applies for several adjacent lines of code. In this case, a blank line should be used to signify that the comment no longer applies to the code after it.
156
+
When describing, say, what the code inside of an if statement does, always begin the comment with two pound signs instead of one. Two `#` indicate that the comment refers to a block of code instead of a single line/command. This is also applicable when making a single comment that applies for several adjacent lines of code. In this case, a blank line should be used to signify that the comment no longer applies to the code after it.
157
157
158
158
```bash
159
159
## Describe what the code inside the if statement does.
@@ -177,7 +177,7 @@ In the case of an if statement containing an `elif` or `else`, three pound signs
177
177
178
178
```bash
179
179
### Describe what the entire if statement does.
180
-
if [[ ...code to be commented on... ]];then
180
+
if [[ ... ]];then
181
181
...code to be commented on...
182
182
else
183
183
...code to be commented on...
@@ -196,21 +196,20 @@ fi
196
196
197
197
## Bashisms
198
198
199
-
This style guide is for bash. This means when given a choice, always prefer
200
-
bash builtins or keywords instead of external commands or `sh(1)` syntax.
199
+
This style guide is for bash. This means when given a choice, always prefer bash builtins or keywords instead of external commands or `sh(1)` syntax.
201
200
202
201
### `test(1)`
203
202
204
203
Use `[[ ... ]]` for conditional testing, not `[ .. ]` or `test ...`.
205
204
206
205
```bash
207
-
# Wrong
206
+
# Wrong.
208
207
test -d /etc
209
208
210
-
# Also wrong
209
+
# Also wrong.
211
210
[ -d /etc ]
212
211
213
-
# Right
212
+
# Right.
214
213
[[ -d /etc ]]
215
214
```
216
215
@@ -223,22 +222,22 @@ Use bash builtins for generating sequences.
223
222
```bash
224
223
n=10
225
224
226
-
## Wrong
225
+
## Wrong.
227
226
forfin$(seq 1 5);do
228
227
...
229
228
done
230
229
231
-
## Wrong
230
+
## Wrong.
232
231
forfin$(seq 1 "$n");do
233
232
...
234
233
done
235
234
236
-
## Right
235
+
## Right.
237
236
forfin {1..5};do
238
237
...
239
238
done
240
239
241
-
## Right
240
+
## Right.
242
241
for((i =0; i < n; i++));do
243
242
...
244
243
done
@@ -249,8 +248,8 @@ done
249
248
Use `$(...)` for command substitution.
250
249
251
250
```bash
252
-
foo=`date`# Wrong
253
-
foo=$(date)# Right
251
+
foo=`date`# Wrong.
252
+
foo=$(date)# Right.
254
253
```
255
254
256
255
### Math / Integer Manipulation
@@ -261,12 +260,12 @@ Use `((...))` and `$((...))`.
261
260
a=5
262
261
b=4
263
262
264
-
## Wrong
263
+
## Wrong.
265
264
if [[ $a-gt$b ]];then
266
265
...
267
266
fi
268
267
269
-
## Right
268
+
## Right.
270
269
if((a > b));then
271
270
...
272
271
fi
@@ -279,13 +278,13 @@ Do **not** use the `let` command.
279
278
Always prefer [parameter expansion](http://mywiki.wooledge.org/BashGuide/Parameters#Parameter_Expansion) over external commands like `echo`, `sed`, `awk`, etc.
280
279
281
280
```bash
282
-
name="bahamas10"
281
+
name="hunter"
283
282
284
-
## Wrong
283
+
## Wrong.
285
284
prog=$(basename "$0")
286
285
nonumbers=$(echo "$name"| sed -e 's/[0-9]//g')
287
286
288
-
## Right
287
+
## Right.
289
288
prog=${0##*/}
290
289
nonumbers=${name//[0-9]/}
291
290
```
@@ -311,13 +310,13 @@ done
311
310
Use bash arrays instead of a string separated by spaces (or newlines, tabs, etc.) whenever possible:
312
311
313
312
```bash
314
-
## Wrong
313
+
## Wrong.
315
314
modules="json httpserver jshint"
316
315
formodulein$modules;do
317
316
npm install -g "$module"
318
317
done
319
318
320
-
## Right
319
+
## Right.
321
320
modules=(json httpserver jshint)
322
321
formodulein"${modules[@]}";do
323
322
npm install -g "$module"
@@ -359,13 +358,13 @@ When writing bash and using all the powerful tools and builtins bash gives you,
359
358
Don't use `cat(1)` when you don't need it. If programs support reading from stdin, pass the data in using bash redirection.
360
359
361
360
```bash
362
-
# Wrong
361
+
# Wrong.
363
362
cat file | grep foo
364
363
365
-
# Right
364
+
# Right.
366
365
grep foo < file
367
366
368
-
#also right
367
+
#Also right.
369
368
grep foo file
370
369
```
371
370
@@ -375,60 +374,57 @@ Prefer the use of the command-line tools built-in method of reading a file inste
375
374
376
375
### Quoting
377
376
378
-
Use double quotes for strings that require variable expansion, command substitution interpolation, and everything in between.
377
+
Use double quotes for strings that require variable expansion, command substitution interpolation, and almost everything in between.
378
+
379
+
Exceptions:
380
+
381
+
1. When you don't want ANY form of variable expansion. Many situations where this is necessary or desired, is when using commands such as `sed` or `grep`.
379
382
380
383
```bash
381
-
## Right
384
+
## Right.
382
385
green=$'\033[0;32m'
383
-
foo="Hello World"
386
+
foo='$USER contains your username'
387
+
bar="You are a user"
384
388
bar="You are $USER"
385
-
bar="You are \$USER"
389
+
bar="\$USER = $USER"
386
390
387
-
## Wrong
391
+
## Wrong.
388
392
foo='Hello World'
389
-
bar='You are $USER'
390
393
```
391
394
392
-
Two exceptions:
395
+
All variables should be quoted, whether or not they undergo word-splitting.
393
396
394
-
1. When using commands that by default require/use/recommend using single quotes, such as grep, sed, and trap.
395
-
2. When saving output formatting (text color, etc.) in variables (i.e. `green=$'\033[0;32m'`)
397
+
Two exceptions:
396
398
397
-
All variables should be quoted, whether or not they undergo word-splitting.
399
+
1. The first exception to this rule is if you call a variable within double brackets, like shown below.
400
+
2. The second exception is if you would like the variable to be ignored if it is empty or does not exist. (Empty or non-existent variables that are quoted leave an empty string where called, while an unquoted variable completely ignores the variable)
398
401
399
402
```bash
400
403
foo="hello world"
401
404
402
-
if [[ -n$foo ]];then# No quotes needed
403
-
404
-
echo"$foo"# Quotes needed
405
+
if [[ -n$foo ]];then# No quotes needed.
406
+
echo"$foo"# Quotes needed.
405
407
fi
406
408
407
-
bar="$foo"# Quotes needed
409
+
bar="$foo"# Quotes needed.
408
410
```
409
411
410
-
Two exceptions:
411
-
412
-
1. The first exception to this rule is if you call a variable within double brackets like shown above.
413
-
2. The second exception is if you would like the variable to be ignored if it is empty or does not exist. (Empty or non-existent variables that are quoted leave an empty string where called, while an unquoted variable completely ignores the variable)
414
-
415
-
416
412
### Variable Declaration
417
413
418
-
Unless exported, all variables should be lowercase. If a variable is being exported, it should be completely uppercase with `_`appended to the beginning of the variable and between each word.
414
+
Unless exported, all variables should be lowercase. If a variable is being exported, it should be completely uppercase with `_`placed to the beginning of the variable and between each word.
419
415
420
416
Don't use `let` or `readonly` to create variables. `declare` should *only* be used for associative arrays. `local` should always be used in functions unless the variable is called outside of the function.
421
417
422
418
```bash
423
-
## Wrong
419
+
## Wrong.
424
420
declare -i foo=5
425
421
let foo++
426
422
readonly bar="something"
427
423
FOOBAR=baz
428
424
export food=5
429
425
export food_cart=5
430
426
431
-
## Right
427
+
## Right.
432
428
i=5
433
429
((i++))
434
430
bar="something"
@@ -439,22 +435,20 @@ export _FOOD_CART=5
439
435
440
436
### shebang
441
437
442
-
Never use `#!/usr/bin/env bash` unless you have a very good reason to. This can cause your scripts to behave differently depending on who runs it. For this reason, use this line instead:
438
+
As a general rule, scripts that are designed to run on anything other than Linux, such as BSD or macOS, should use `#!/usr/bin/env bash`. Otherwise, `#!/bin/bash` should be used instead.
443
439
444
-
```bash
445
-
#!/bin/bash
446
-
```
440
+
The reasoning is that bash is located in a different location on BSD. On macOS, users will often install a newer version of bash via homebrew, as the default version is much older. So it's ideal for the shebang to be more flexible. These are neither of the case for Linux, as bash is generally updated when a new version of the distro is released, and is always located in `#!/bin/bash`.
447
441
448
442
## Error Checking
449
443
450
-
`cd`, for example, doesn't always work. Make sure to check for any possible errors for `cd` (or commands like it) and exit or break if they are present.
444
+
`cd`, for example, doesn't always work. Make sure to check for any possible errors when using `cd` (or commands like it) and perform the proper action.
451
445
452
446
```bash
453
-
## Wrong
454
-
cd /some/path # This could fail
455
-
rm file # If cd fails, where am I? what am I deleting?
447
+
## Wrong.
448
+
cd /some/path # This could fail.
449
+
rm file # If cd fails, where am I? What am I deleting?
0 commit comments