#!/bin/sh
# This is a comment
echo "Hello World!"
cat <<EOF
-------------------------------
This is a multiline
character string
-------------------------------
EOF
<<EOF
HEREDOCS can also be
used to incluide multiline
comments
EOF
: '
Another way to include comments
is using the : operator which
does nothing
'Bash is one of the most popular command interpreters, in part due to the large number of functions it has added over the years compared to the standard shell.
Compared to the other languages we are seeing, the shell has much lower expressive power, however, the chances that at some point you will have to write a program for a shell (what is known as a Shell Script) are very high.
Shell scripts usually use the .sh extension
(derived from SHell obviously).
DID YOU KNOW…
Sometimes you will see shell scripts with other extensions such as
bash. In these cases, the extension usually indicates the type of shell for which the program has been written since it may use a special function of that shell and not work on others. Theshextension indicates that the standard shell found in/bin/shis used and is calledsh.
As we can see, the character # is used to add comments
and the echo instruction to print strings on the screen.
Note that although quotes have been used to delimit the string of
characters, in that specific case it could have been omitted.
echo by default adds a carriage return to the string.
I’M A NEWBIE
The difference between
echo "Hello World"andecho Hello Worldis that in the first case we are passing a single parameter to theechoprogram while in the second case we are passing two. Theechoprogram prints on the screen all parameters it receives separated by a space. Try adding more than one space betweenHelloandWorldin the two previous cases and you will see what happens.
We can also use here-docs and define multi-line comments
using here-docs that we don’t pass as a parameter to
anything (in fact, we can also do this in Ruby) or using the
: operator.
As with all interpreted languages, to run our program we must give it
permissions and use the interpreter if we haven’t added the
she-bang.
$ bash hello.sh
$ chmod +x hello.sh
$ ./hello.sh
Bash was developed by Brian Fox for the GNU project to replace the
Bourne Shell, which was proprietary. Its first version dates back to
1989, and since then it has become the standard shell for many systems.
In recent years, other shells (dash, zsh,…)
have begun to be used by, especially, some Linux distros, but Bash
remains the most widespread.
DID YOU KNOW…
Bash is an acronym for Bourne-again SHell, which is a pun on the name of the shell it’s trying to replace (Bourne Shell) and the idea of being reborn (Born again).
Summary
- Interpreted language
- Extension:
sh/bash - Line terminator: Carriage return
- Groups instructions with: Depends on the command
- Multi-line comments: Possible using
HERE-DOCS - Single-line comments:
# - String delimiter:
"string"or'string' - You can print text with
echo(can add (by default) or not carriage return) - Supports multi-line strings: Depends on the operation
- Supports
HERE-DOCS - Entry point: Beginning of the file