Specify bash
Shell script first line, #! What should it be?
Most of the cases/usr/bin/env bash,/bin/bash,/usr/bin/bash,/bin/sh,/usr/bin/env sh are equivalent, but few cases still have pits. If the default shell may not be bash, such as a release law, the default SH is not bash.
So we recommend using/bin/bash
Set-e and Set-x
Insert a line of SET-E and a line before the specific code logic set-x
Set-x will output the execution when executing each line of the shell script. All variables involved will be replaced with actual values.
SET-E will end the program in the execution of an error. (but not all errors will end the program).
Bring Shellcheck.
Shellcheck can check the wrong
Note Local
When writing a function, if you do not add the local qualifier, then the default is the global variable, which is similar to JS.
In a top-level scope, there is no global impact, but in a function, a global variable can contaminate other scopes. So the variables declared in the function must be prefixed with the local qualifier.
Complex logic or avoiding the shell
Shell scripting is often difficult to transplant, it is difficult to unify the error processing, difficult to process data. So you need to work with a lot of external commands, such as Grep,sed,awk. So solving a particular problem requires a specific tool, switching to a more generic scripting language, sometimes better.
Shell's recommendations