Skip to content

Commit e3412ec

Browse files
author
Hunter T
committed
Update README.md
1 parent f92ec05 commit e3412ec

File tree

1 file changed

+67
-71
lines changed

1 file changed

+67
-71
lines changed

README.md

Lines changed: 67 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@ It's preferred that columns are 88 characters or less. The only reason that the
3535
Do not use semicolons where they are not needed.
3636

3737
``` bash
38-
## Wrong
38+
## Wrong.
3939
name="dave";
4040
echo "hello $name";
4141

42-
## Right
42+
## Right.
4343
name="dave"
4444
echo "hello $name"
4545

46-
# Also right
46+
# Also right.
4747
name="dave"; echo "hello $name"
4848
```
4949

@@ -56,12 +56,12 @@ Don't use the `function` keyword. Instead, `()` should be appended to the end of
5656
``` bash
5757
## Wrong, if $i isn't used outside of 'foo'.
5858
function foo {
59-
i=foo # This is now global, wrong depending on intent
59+
i=foo # This is now global, wrong depending on intent.
6060
}
6161

62-
## Right
62+
## Right.
6363
foo() {
64-
local i=foo # This is local, preferred
64+
local i=foo # This is local, preferred.
6565
}
6666
```
6767

@@ -70,36 +70,36 @@ foo() {
7070
`then` should be on the same line as `if`, and `do` should be on the same line as `while`.
7171

7272
``` bash
73-
## Wrong
73+
## Wrong.
7474
if true
7575
then
7676
...
7777
fi
7878

79-
## Also wrong
79+
## Also wrong.
8080
true && {
8181
...
8282
}
8383

84-
## Right
84+
## Right.
8585
if true; then
8686
...
8787
fi
8888
```
8989

9090
### Single line block statements
9191

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.
9393

9494
```bash
95-
## Wrong
95+
## Wrong.
9696
if [[ -f $file_name ]]; then echo "$file_name exists!"; fi
9797

98-
## Also wrong
98+
## Also wrong.
9999
if [[ -f $file_name ]]; then echo "$file_name exists!"; echo "Done"
100100
fi
101101

102-
## Right
102+
## Right.
103103
if [[ -f $file_name ]]; then echo "$file_name exists!"
104104
fi
105105
```
@@ -137,13 +137,13 @@ func() {
137137
}
138138
```
139139

140-
#### Number of pound signs
140+
#### Number of pound signsNumber of pound signs
141141

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.
143143

144144
```bash
145145
# Contains the raw URL link to this 'README.md'.
146-
export _RAW_URL="https://raw.githubusercontent.com/StrangeRanger/bash-style-guide/master/README.md"
146+
export _RAW_URL="https://raw.githubusercontent.com/StrangeRanger/bash-style-guide/main/README.md"
147147
```
148148

149149
```bash
@@ -153,7 +153,7 @@ if [[ ...code to be commented on... ]]; then
153153
fi
154154
```
155155

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.
157157

158158
```bash
159159
## 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
177177

178178
```bash
179179
### Describe what the entire if statement does.
180-
if [[ ...code to be commented on... ]]; then
180+
if [[ ... ]]; then
181181
...code to be commented on...
182182
else
183183
...code to be commented on...
@@ -196,21 +196,20 @@ fi
196196

197197
## Bashisms
198198

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.
201200

202201
### `test(1)`
203202

204203
Use `[[ ... ]]` for conditional testing, not `[ .. ]` or `test ...`.
205204

206205
``` bash
207-
# Wrong
206+
# Wrong.
208207
test -d /etc
209208

210-
# Also wrong
209+
# Also wrong.
211210
[ -d /etc ]
212211

213-
# Right
212+
# Right.
214213
[[ -d /etc ]]
215214
```
216215

@@ -223,22 +222,22 @@ Use bash builtins for generating sequences.
223222
``` bash
224223
n=10
225224

226-
## Wrong
225+
## Wrong.
227226
for f in $(seq 1 5); do
228227
...
229228
done
230229

231-
## Wrong
230+
## Wrong.
232231
for f in $(seq 1 "$n"); do
233232
...
234233
done
235234

236-
## Right
235+
## Right.
237236
for f in {1..5}; do
238237
...
239238
done
240239

241-
## Right
240+
## Right.
242241
for ((i = 0; i < n; i++)); do
243242
...
244243
done
@@ -249,8 +248,8 @@ done
249248
Use `$(...)` for command substitution.
250249

251250
``` bash
252-
foo=`date` # Wrong
253-
foo=$(date) # Right
251+
foo=`date` # Wrong.
252+
foo=$(date) # Right.
254253
```
255254

256255
### Math / Integer Manipulation
@@ -261,12 +260,12 @@ Use `((...))` and `$((...))`.
261260
a=5
262261
b=4
263262

264-
## Wrong
263+
## Wrong.
265264
if [[ $a -gt $b ]]; then
266265
...
267266
fi
268267

269-
## Right
268+
## Right.
270269
if ((a > b)); then
271270
...
272271
fi
@@ -279,13 +278,13 @@ Do **not** use the `let` command.
279278
Always prefer [parameter expansion](http://mywiki.wooledge.org/BashGuide/Parameters#Parameter_Expansion) over external commands like `echo`, `sed`, `awk`, etc.
280279

281280
``` bash
282-
name="bahamas10"
281+
name="hunter"
283282

284-
## Wrong
283+
## Wrong.
285284
prog=$(basename "$0")
286285
nonumbers=$(echo "$name" | sed -e 's/[0-9]//g')
287286

288-
## Right
287+
## Right.
289288
prog=${0##*/}
290289
nonumbers=${name//[0-9]/}
291290
```
@@ -311,13 +310,13 @@ done
311310
Use bash arrays instead of a string separated by spaces (or newlines, tabs, etc.) whenever possible:
312311

313312
``` bash
314-
## Wrong
313+
## Wrong.
315314
modules="json httpserver jshint"
316315
for module in $modules; do
317316
npm install -g "$module"
318317
done
319318

320-
## Right
319+
## Right.
321320
modules=(json httpserver jshint)
322321
for module in "${modules[@]}"; do
323322
npm install -g "$module"
@@ -359,13 +358,13 @@ When writing bash and using all the powerful tools and builtins bash gives you,
359358
Don't use `cat(1)` when you don't need it. If programs support reading from stdin, pass the data in using bash redirection.
360359

361360
``` bash
362-
# Wrong
361+
# Wrong.
363362
cat file | grep foo
364363

365-
# Right
364+
# Right.
366365
grep foo < file
367366

368-
# also right
367+
# Also right.
369368
grep foo file
370369
```
371370

@@ -375,60 +374,57 @@ Prefer the use of the command-line tools built-in method of reading a file inste
375374

376375
### Quoting
377376

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`.
379382

380383
``` bash
381-
## Right
384+
## Right.
382385
green=$'\033[0;32m'
383-
foo="Hello World"
386+
foo='$USER contains your username'
387+
bar="You are a user"
384388
bar="You are $USER"
385-
bar="You are \$USER"
389+
bar="\$USER = $USER"
386390

387-
## Wrong
391+
## Wrong.
388392
foo='Hello World'
389-
bar='You are $USER'
390393
```
391394

392-
Two exceptions:
395+
All variables should be quoted, whether or not they undergo word-splitting.
393396

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:
396398

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)
398401

399402
``` bash
400403
foo="hello world"
401404

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.
405407
fi
406408

407-
bar="$foo" # Quotes needed
409+
bar="$foo" # Quotes needed.
408410
```
409411

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-
416412
### Variable Declaration
417413

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.
419415

420416
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.
421417

422418
``` bash
423-
## Wrong
419+
## Wrong.
424420
declare -i foo=5
425421
let foo++
426422
readonly bar="something"
427423
FOOBAR=baz
428424
export food=5
429425
export food_cart=5
430426

431-
## Right
427+
## Right.
432428
i=5
433429
((i++))
434430
bar="something"
@@ -439,22 +435,20 @@ export _FOOD_CART=5
439435

440436
### shebang
441437

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.
443439

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`.
447441

448442
## Error Checking
449443

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.
451445

452446
``` 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?
456450

457-
## Right
451+
## Right.
458452
cd /some/path || exit
459453
rm file
460454
```
@@ -475,6 +469,8 @@ http://mywiki.wooledge.org/BashFAQ/105
475469

476470
Never.
477471

472+
https://stackoverflow.com/questions/17529220/why-should-eval-be-avoided-in-bash-and-what-should-i-use-instead
473+
478474
### Redirecting Errors
479475

480476
When echoing an error message, always redirect the message to stderr:

0 commit comments

Comments
 (0)