Bash Scripting
Last updated
Was this helpful?
Last updated
Was this helpful?
"shell scripting tutorial"
"aid people interested in learning to work with BASH"
devhints.io
"a summary of everything that can, and should be done when writing shell scripts."
"static analysis tool for shell scripts"
"causes bash to behave in a way that makes many classes of subtle bugs impossible."
While bash
is "sh-compatible" some features of bash
will break or cause unexpected behaviour in sh
.
Don't give scripts an .sh
extension.
It's a bash
script, not an sh
script
If you're piping to grep
multiple times only the last grep
in the sequence can be called with -q
.
First line of the script that indicates which interpreter is used to execute the file. The #!
must be at the very start of the file, with no spaces or blank lines before it.
"#!/usr/bin/env
searches PATH
for bash
, and bash
is not always in /bin
, particularly on non-Linux systems."
"This way, you don't have to look for it in a specific place on the system, as those paths may be in different locations on different systems. As long as it's in your path, it will find it."
Execute bash
and tell it to read the script myscript
. When executing the script this way the shebang line (#!
) is just a comment, bash
does nothing with it
We can give the script executable permission. Instead of calling bash
manually, we can execute myscript
directly.
When myscript
is executed this way, the shebang line (#!
) is used to determine which interpreter to use.
Print every command before its execution, replacing the variables with their real values.
by
Read for a more detailed explanation.
The advises against it unless it's a library