https://github.com/rubenhortas/bash_scripting_examples
Examples of bash scripting
https://github.com/rubenhortas/bash_scripting_examples
bash bash-script bash-scripting examples scripting
Last synced: 3 months ago
JSON representation
Examples of bash scripting
- Host: GitHub
- URL: https://github.com/rubenhortas/bash_scripting_examples
- Owner: rubenhortas
- Created: 2023-01-09T17:45:30.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2025-05-11T16:11:34.000Z (7 months ago)
- Last Synced: 2025-06-29T06:42:31.776Z (6 months ago)
- Topics: bash, bash-script, bash-scripting, examples, scripting
- Language: Shell
- Homepage:
- Size: 39.1 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Bash examples
Small examples of bash scripting.
## Bash scripting naming conventions
| Element |Notation |Example |Notes |
|----------------------------|----------------------|---------------------|-------------------------------------------------------------------------------------------------------------------------|
| Constants | SCREAMING_SNAKE_CASE | DEST_PATH | Use `readonly` or `declare -r` to ensure they are readonly. |
| Environment variable names | SCREAMING_SNAKE_CASE | PATH | |
| File | snake_case | my_script | Executables should not have extension (strongly preferred) or a `.sh` extension.[^1] |
| Functions | snake_case | do_something(){ } | The keyword `function` it's optional, but must be used consistently troughout a project.[^2] |
| Hashbang | | #!/usr/bin/env bash | #!/usr/bin/bash asumes it's always installed in /bin, which can cause issues.[^3] |
| Local variables | snake_case | my_local_variable | Ensure that local variables are only seen inside a function and it's children by using `local` when declaring them. |
| Variables | snake_case | user_name | |
## Multiline comments
```bash
: '
line1
line2
'
```
## Debug
### Executing
```bash
bash -x ./script
```
### With hashbang
```bash
#/usr/bin/env bash -x
```
### Debug a block
```bash
set -x
echo "Code block"
set +x
```
## Command calls
### Check return values
Always check return values and give informative return values.
For unpiped commands use `$?` or check directly via an `if` statement.
```shell
if ! mv "${file_list[@]}" "${dest_dir}/"; then
echo "Unable to move ${file_list[*]} to ${dest_dir}" >&2
exit 1
fi
# Or
mv "${file_list[@]}" "${dest_dir}/"
if (( $? != 0 )); then
echo "Unable to move ${file_list[*]} to ${dest_dir}" >&2
exit 1
fi
```
### Builtin Commands vs External Commands
Given the choice between invoking a shell builtin and invoking a separete process, choose the builtin.
```shell
# Prefer this:
addition=$(( X + Y ))
substitution="${string/#foo/bar}"
# Instead of this:
addition="$(expr "${X}" + "${Y}")"
substitution="$(echo "${string}" | sed -e 's/^foo/bar/')"
```
## Static analysis
Use [ShellCheck](https://github.com/koalaman/shellcheck) to get warnings and suggestions for your scripts.
## Sources
* [Google Shell Style Guide](https://google.github.io/styleguide/shellguide.html)
[^1]: Libraries must have a `.sh`extension and should not be executable.
[^2]: The use of the keyword `function`reduces compatibility with older versions of bash.
[^3]: Google does recommend `#!/bin/bash`
## Support
If you find these examples useful, you can star this repo.