bash
Is this section core or elective? | Expected time to completion |
---|---|
core | before second meeting |
bash
is a shell, or command-line interface (CLI), which is a computer program
exposing the operating system’s (OS) services to the user. Its purpose is to
interactively execute low-level operations and to allow their automation in
scripts.
bash
is the default shell for most UX (Unix or similar) systems. Its name is
an abbreviation for Bourne again shell. (It was developed as a free
software replacement of the Bourne shell (named after its creator Stephen
Bourne), which at the time was the standard Unix shell; hence Bourne again.)
This chapter introduces the basics of bash
. To do that, the main ingredients
shell commands and shell variables are explained, and the basic bash
fundamentals are described.
bash
scripts are typically used for low-level operations, such as:
Nowadays, users interactively performing any of these tasks will use a graphical
user interface (GUI). However, this is impractical when the tasks are repetitive
and affect many components. In this case, it is a good practice to automate the
tasks, and this is where shells such as bash
come in. Thus, by combining
operations of the type listed above, bash
can be used to automate complex
set-up and deployment processes, and this is where its main use cases lie.
In this course, we will mainly use bash
to operate the version-control system
git
, but this is a good opportunity to get know the bare basics of bash
.
The basic form of interaction is via commands at the command prompt. In this
section, we will assume that you have followed the instructions in the
previous section to install git
and a
bash
-compatible terminal emulator. You may start up bash
by typing bash
in the start menu. Try the following commands; be careful
when using commands that modify the file system!
echo <str>
print a string (i.e. display it in the terminal window)date
return current date and timepwd
print working directoryls
list contents of working directorycat <file>
print contents of a filecd <dir>
changes working directorytouch <file>
create a filevim <file>
create a new or edit an existing file in vim. Note: How to use vim.mkdir <dir>
create a directorycp <file0> <file1>
copy a file/foldermv <file0> <file1>
move/rename a file/folderrm <file>
remove a filermdir <dir>
remove a directoryThe options shown above are very basic; each of the commands listed offers a
wealth of additional functionality through various options. Try calling any
of the commands with the option --help
to find out more about these; e.g.,
touch --help
.
bash
scriptingbash
scripts, simply put, are sequences of commands which might also be issued
in an interactive session.
It is considered good practice to start every bash
script with a line which
specifies the location of the bash
executable via a
shebang. Typically, this line
has the following form:
#!/bin/bash
Scripts that exhibit such a first line can be executed by their name from the
bash
prompt
./<filename.sh>
In general, line comments begin with the hash sign #
. Unless it is followed
by an exclamation mark (turning it into a shebang), we may follow the line
comment with an arbitrary text, which will be disregarded during script
execution and serves only documentation purposes for human readers.
A bash
script can be called in the following form:
bash <filename.sh> <optional_argument_1> ... <optional_argument_n>
(Or, alternatively, if the shebang convention noted above is followed, by
./<filename.sh> <optional_argument_1> ... <optional_argument_n>
.)
Observe that you can pass arguments to the script. They can be accessed within the script, in the following way:
$0
corresponds to script name$1
to $n
corresponds to argument 1 to n$$
corresponds to process id$*
or $@
corresponds to all argument values$#
corresponds to total number of argumentsFor example, let’s assume we write a script called test.sh
with the following
contents:
#!/bin/bash
# I am a comment by d-fine bash_basics training
echo "Hello world, this script is called $0."
This file can be created using bash with the vim editor - please refer to the command list above.
You can call this script by simply clicking on it or by issuing ./test.sh
on the command line when in the same folder, bash pathTo/test.sh
otherwise.
Shell variables allow you to store information during script execution, and to
re-use that information. There are no data types. Variables can be assigned
using VAR=<value>
, and their value can be retrieved using $VAR
. The
convention is to use upper case letters for variables. For example:
HELLO="Hello"
echo $HELLO
The $
syntax can also be applied to more complex commands by enclosing them in
parentheses, viz.
echo $(date)
prints the return value of the date
command. This can also be used to assign
the value of a script command to a shell variable, following the pattern
FOO=$(foo)
.
A shell script always has an exit code. Exit codes are unsigned integers and are
used to report success or failures, where the convention is that 0 represents
success, and any other value is failure. The precise encoding of different types
of failure may vary. However, as any script might be called by another script
which evaluates the exit code, one should always report the outcome of the
script execution. This can be done via the word exit and a corresponding exit
code number, e.g. exit 0
. If exit
is not called, the exit code of the script
will be equal to the exit code of the last command called.
Remember how to submit solutions.
bash
script that prints “Hello World” to the console
when executed.bash
script that prints “Hello <Username>” to the console when
executed, where <Username> is a placeholder for the logged in user. (Hint:
whoami
)