You can accomplish many system administration tasks by using command-line tools. More complex tasks often require chaining together multiple commands that pass results between them. By using the Bash shell environment and scripting features, you can combine Linux commands into shell scripts to solve repetitive real-world problems.
A Bash shell script is an executable file that contains a list of commands, and possibly with programming logic to control decision-making in the overall task. When well-written, a shell script is a powerful command-line tool on its own, and you can use it with other scripts.
Shell scripting proficiency is essential for system administrators in any operational environment. You can use shell scripts to improve the efficiency and accuracy of routine task completion.
<aside> 🥱 • Devhints Bash Cheat Sheet
</aside>
Great to see you're working with Bash shell syntax. Just a quick tip - while any text editor will do, more advanced editors like vim
or emacs
can really enhance your experience. They even provide color-coded highlighting that can help you spot common scripting errors such as improper syntax, unmatched quotes, parentheses, brackets, braces, and other structural mistakes. Happy scripting!
We start by creating a file with the .sh
extension. For example, script.sh
. Then we write the script in it.
So, basically, when you start writing a script, the first thing you gotta do is begin with the… #!
notation, which is commonly referred to as she-bang
or hash-bang
, from the names of those two characters, sharp
or hash
and bang
. This notation is an interpreter directive to indicate the command interpreter and command options to process the remaining lines in the file. The shebang for python is #!/usr/bin/env python
. It varies from language to language. For Bash syntax script files, the first line is the following directive:
#!/usr/bin/bash
echo "Hello World"
We can run this by bash script.sh
or ./script.sh
. But the second will only work if the script is executable (permission to execute). We can make it executable by chmod +x script.sh
. Now we can run it by ./script.sh
.
If the script is stored in a directory that is listed in the shell's PATH
environmental variable, then you can run the shell script by using only its file name, similar to running compiled commands. Because PATH parsing runs the first matching file name that is found, always avoid using existing command names to name your script files.
Quote Special Characters:
[user@host ~]$ echo # not a comment #
[user@host ~]$ echo \\# not a comment #
# not a comment
[user@host ~]$ echo \\# not a comment \\#
# not a comment #
[user@host ~]$ echo '# not a comment #'
# not a comment #
Some characters and words have special meanings in the Bash shell. To use them with their literal values instead of their special meanings, you need to escape them in the script. You can escape them using the backslash character (\\\\
), single quotes (''
), or double quotes (""
).
Provide Output from a Shell Script
The echo
command displays arbitrary text by passing the text as an argument to the command. By default, the text is sent to standard output (STDOUT). You can send text elsewhere by using output redirection. In the following simple Bash script, the echo
command displays the "Hello, world"
message to STDOUT, which defaults to the screen device.
[user@host ~]$ cat ~/bin/hello
#!/usr/bin/bash
echo "Hello, world"
[user@host ~]$ hello
Hello, world
Note : This user can run hello
at the prompt because the ~/bin
(/home/user/bin
) directory is in the user's PATH
variable and the hello
script has executable permission. The PATH parser finds the script first, if no other executable file called hello
is found in any earlier PATH
directory. Your home directory's bin
subdirectory is intended to store your personal scripts.
The
echo
command is widely used in shell scripts to display informational or error messages. Messages helpfully indicate a script's progress, and can be directed to standard output or standard error, or be redirected to a log file for archiving. When you display error messages, good programming practice is to redirect error messages to STDERR to separate them from normal program output.
[user@host ~]$ cat ~/bin/hello
#!/usr/bin/bash
echo "Hello, world"
echo "ERROR: Houston, we have a problem." >&2
[user@host ~]$ hello 2> hello.log
Hello, world
[user@host ~]$ cat hello.log
ERROR: Houston, we have a problem.