1. Check your path to the command interpreter - the first line is important #!/bin/bash. Make sure that /bin/bash actually points to the interpreter. How? Use ‘ls’?
2. Line endings matter! If you’re writing bash in a Windows editor and transferring that file to Linux the bash interpreter will spit it out. Use dos2unix to strip out Windows line endings.
3. The best bash scripts are SHORT bash scripts, keep them single-use and small.
4. Always use an EXIT CODE. Make sure you have an exit 0 at the end of your scripts, use an Exit 1 in any failure mode. Other scripts that rely on the one you’re writing will get helpful feedback and stop. This is DOUBLY important for embedding scripts in Continuous Integration (CI) tools like gitlab or github actions.
5. Validate the input strings to shell scripts and validate environment variables. Always check for the existence of variables and that they are not empty. Make your script fail with an error if arguments or variables don’t pass validation.
Ещё видео!