Skip to content
This repository has been archived by the owner on Mar 7, 2024. It is now read-only.

Commit

Permalink
Improve wording
Browse files Browse the repository at this point in the history
  • Loading branch information
lildude committed Apr 6, 2018
1 parent ef28e63 commit 0f604a5
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions STYLEGUIDE.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
## Bash Styleguide
## Bash Style Guide

If you've not done much Bash development before you may find these debugging tips useful: http://wiki.bash-hackers.org/scripting/debuggingtips.

---
##### Scripts must start with `#!/usr/bin/env bash`

---
##### Scripts must use `set -e`
##### Use `set -e`

If the return value of a command can be ignored, suffix it with `|| true`:

Expand All @@ -19,7 +19,7 @@ command_that_should_not_fail
Note that ignoring an exit status with `|| true` is not a good practice though. Generally speaking, it's better to handle the error.

---
##### Scripts should not check exit status via `$?` manually
##### Avoid manually checking exit status with `$?`

Rely on `set -e` instead:

Expand All @@ -40,7 +40,7 @@ fi
```

---
##### Scripts must include a usage, description and optional examples
##### Include a usage, description and optional examples

Use this format:

Expand Down Expand Up @@ -87,15 +87,15 @@ fi
```

---
##### Scripts should not use Bash arrays
##### Avoid Bash arrays

Main issues:

* Portability
* Important bugs in Bash versions < 4.3

---
##### Scripts should use `test` or `[` whenever possible
##### Use `test` or `[` whenever possible

```bash
test -f /etc/passwd
Expand Down Expand Up @@ -132,7 +132,7 @@ done
```

---
##### Scripts should use `$[x+y*z]` for mathematical expressions
##### Use `$[x+y*z]` for mathematical expressions

```bash
local n=1
Expand All @@ -144,7 +144,7 @@ n=$(($n+1))
```

---
##### Scripts should use variables sparingly
##### Use variables sparingly

Short paths and other constants should be repeated liberally throughout code since they
can be search/replaced easily if they ever change.
Expand All @@ -163,7 +163,9 @@ rsync /data/user/db remote:/data/user/db
```

---
##### Scripts should use lowercase variables for locals, and uppercase for variables inherited or exported via the environment
##### Use lowercase and uppercase variable names

Use lowercase variables for locals and internal veriables, and uppercase for variables inherited or exported via the environment

```bash
#!/usr/bin/env bash
Expand All @@ -176,7 +178,7 @@ git rev-list
```

---
##### Scripts should use `${var}` for interpolation only when required
##### Use `${var}` for interpolation only when required

```bash
greeting=hello
Expand All @@ -185,7 +187,7 @@ echo ${greeting}world
```

---
##### Scripts should use functions sparingly, opting for small/simple/sequential scripts instead whenever possible
##### Use functions sparingly, opting for small/simple/sequential scripts instead whenever possible

When defining functions, use the following style:

Expand All @@ -198,7 +200,7 @@ my_function() {
```

---
##### Scripts should use `<<heredocs` when dealing with multi-line strings
##### Use `<<heredocs` when dealing with multi-line strings

- `<<eof` and `<< eof` will allow interpolation
- `<<"eof"` and `<<'eof'` will disallow interpolation
Expand All @@ -220,7 +222,7 @@ eof
```

---
##### Scripts should quote variables that could reasonably have a space now or in the future
##### Quote variables that could reasonably have a space now or in the future

```bash
if [ ! -z "$packages" ]; then
Expand All @@ -229,7 +231,7 @@ fi
```

---
##### Scripts should use two space indentation
##### Use two space indentation

---
##### Scripts should not produce errors or warnings when checked with ShellCheck
Expand All @@ -238,7 +240,7 @@ Use inline comments to disable specific tests, and explain why the test has been

```bash
hexToAscii() {
# shellcheck disable=SC2059 # $1 needs to be interpretted as a formatted string
# shellcheck disable=SC2059 # $1 needs to be interpreted as a formatted string
printf "\x$1"
}
```
Expand Down

0 comments on commit 0f604a5

Please sign in to comment.