November 29, 2022
Some basic bash-script code example. It might be help if writing some temporary work-scripts.
Assignment and Substitution
Variables
- Built-in Variables
For example$HOME $PWD ...
, for more info, see environ(7) - Positional Parameters
echo para2 para4 1 3 @ - Special Parameters
$? # exit status of a command, function, or the script itself
Branches
Loops
range for
for in C-like syntax
while
IO
Function
Debugging
- take good use of sh(1)
for example:
sh -n script: checks for syntax
sh -v script: echo each command before executing it
sh -x script: echo the result of each command in an abbreviated manner - use echo
- use trap
Parallel
use GNU parallel
Script with Style
- Comment your code
- Avoid using magic number
- Use exit codes in a systematic and meaningful way
- Use standardized parameter flags for script invocation