2 1 Scripting Introduction To Bash
2 1 Scripting Introduction To Bash
| Pipes
~ Tilde Expansion
{ } Brace Expansion
${ } Parameter Expansions
Limitations
This assignment is an overview of important scripting concepts that
learners should be familiar with before attempting upcoming
assignments.
What is Bash?
Bash is a shell, a program that lets us interact with the computer. The
name “Bash” is short for B.ourne A.gain SH.ell.
Many times, we’ll type Bash commands one-by-one in the terminal. We can
also create and run bash scripts, which are files that contain several lines
of bash commands and can be run like any other program.
Bash scripts allow us to build logic, text formatting, and other helpful tools
into files that we can distribute or use to automate command execution.
Let’s make sure we have Bash installed on our system before we can use its
tools. It’s pre-installed in most Linux systems, but it’s always good to check
and be sure that it’s available.
bash --version
bash version
When a user opens a terminal, the default shell opens in the window. The
$SHELL environmental variable shows us exactly what shell is open.
Copy the line into the terminal and press return to see
which shell we’re using:
echo $SHELL
Checkpoint
Global
A globally scoped environment variable can be accessible from anywhere
in that terminal’s environment. It may be used in any script, application, or
process connected to that terminal.
export NAME=value
export IRONMAN=Tony
We can recall the value of this variable using echo and the dollar sign $.
echo $IRONMAN
The set command will show us global and local environmental variables
that are defined in the shell.
Click the button below to run the set command and see that
IRONMAN is included in this list.
unset IRONMAN
Local
Locally scoped environment variables are not accessible to every
application or process running in the terminal. It can only be accessed by
the terminal that defined it.
We can set a new local environment variable using the following syntax.
NAME=value
NAME=''
We can use the printenv command to display all of the local environment
variables that are currently defined.
printenv
Here are a few more variables that you may encounter. Feel free to
retrieve their values in the terminal using echo $.
Checkpoint
There are two types of file paths: absolute paths and relative paths.
Absolute Path
An absolute path defines a location from the highest level, or root, of the
file system, indicated by a slash /, to a certain directory or file. These are
useful when we want to prevent confusion about the location of a file or
directory.
Click the button below print the absolute path to our current working
directory in the terminal with the pwd command.
We can also specify the absolute path to a file within the current directory.
For example, the absolute path from the root directory to the file
champs.txt would be:
/home/codio/workspace/champs.txt
info
## Notice
Both of these paths begin with \, meaning they being at the root
directory.
The user’s home directory can be referred to with the tilde character ~.
This allows us to define absolute file paths without knowing the exact name
of the user’s home directory.
~/workspace/champs.txt
Relative Filename
A relative path is a portion of a path that starts from the user’s current
working directory rather than the root directory.
Relative paths do not start at the root directory, so they do not start with a
slash /.
/home/codio/workspace
ExampleFolder
cd ..
pwd
If we were to change our working directory, the relative path to the same
folder would change in relation to our new folder. The relative path to
ExampleFolder from our new directory would be:
workspace/ExampleFolder
Checkpoint
Fill in the blanks to complete the statement below.
Built-In Bash Commands
Using a shell, like bash, involves running commands like:
- ls
- rm
- grep
- arc
echo
The echo command displays text to the terminal. Use the button below to
run this command with the text Hey, all you people in the terminal.
Because echo ends a line with a new line character, it can be used to output
text to the user without issue.
printf
The printf built-in also displays text, but without the new line character at
the end. Use the button below to run printf with the string input "Won't
you listen to me?".
source
cat commands.txt
Let’s perform the commands within the file commands.txt using the source
command.
source /home/codio/workspace/commands.txt
man bash
Checkpoint
Click the Try It! button to run your script in the terminal
and check your output. It should look similar to the output
below.
When you are finished writing your script, click the Check
It! button to check your work.
Redirection
##
Redirection and Piping are two ways we can control command input and
output in Bash.
.guides/img/outputRedirect
Let’s take the results of the pwd command and redirect it to the file
directory.txt using the > character.
cat directory.txt
ls >> directory.txt
cat directory.txt
These data streams are given unique file descriptor numbers. We can
access the data by referring to its file descriptor number as described by
the table below.
stdout 1
stderr 2
ls /fakedir
cat output.txt
cat error.txt
Input redirection < can also be used to extract data from a file and seem as
if it were entered as standard input.
.guides/img/inputRedirect
In this example, we’ll use input redirection to use the output of champs.txt
as input to the cat command.
When using a here document, make sure that the specified string is
unique so it isn’t confused for any other line in the input.
Checkpoint
1. Redirect the results of echo "Disk Space Usage Report" into a file called
diskspace.txt.
2. Append the results of the df command onto diskspace.txt
3. Redirect the output of diskspace.txt into the terminal display using cat
4. Redirect the following lines into the terminal display using cat and the
delimiter EndOfReport.
I have reported the usage of disk space in KBs.
If you'd like, I can also run the report in megabytes
It would be as easy as using the df -m command
Piping
Piping
Piping connects the output of two processes by sending the first process’
output to the second process as input, without being displayed on the
screen.
As long as the output of one command can be used as input for another,
pipes (|) allow you to chain commands. They are used in Bash scripting to
create custom flows.
.guides/img/pipe
We can use the cat command to look at the contents of a file champs.txt
that holds every winning NFL Super Bowl team in history.
cat champs.txt
This is a lot of information. We can pipe (|) the output of the previous
command into the less command to display this information page by page.
You can use the UP or DOWN arrows to move up and down the file line by
line.
cat champs.txt | wc
You can string many pipes together, as long as the output of a command
makes sense as input for the next command.
Piping and redirection seem similar, but we use them differently. Mainly,
piping sends information from one process to another, while redirection
sends information to and from files.
For reference, you can find the different characters we use for redirection
and piping below.
Checkpoint
5. Using the cat command, pipe the contents of the file alpha-
winners.txt into the less command
6. Click the Try It! button to run your script in the terminal and check
your output. It should look similar to the output below.
#
When you are finished writing your script, click the Check
It! button to check your work.
Tilde Expansion
It’s common to need to use unknown values when we work with Bash. For
example, we might need to use the path to a user’s home folder, the user’s
own data, or the result of a calculation.
In Bash, you can use expansions and substitutions to show these values.
When they are run, they are read and replaced with a value or group of
values.
Tilde Expansion ~
The tilde expansion is used to represent a user’s home directory. Type the
following in the terminal to display your home directory.
echo ~
/home/codio
We can use ~ and the name of a user if we want to retrieve the home
directory of a specific user.
echo ~codio
We can use tilde + plus ~+ to represent the PWD, or to present the current
directory that we’re working in.
Checkpoint
echo /tmp/{one,two,three}/file.txt
We can use it to present a set of values to use in the same part of a string.
echo r{i,o,u}se
echo {1..10}
echo {a..z}
We can also add intervals to sequences with a third argument in our brace
expansion. The following command displays numbers in intervals
of .
echo {0..20..2}
We can create a few files neatly labeled filed with sequential numbering
and lettering.
touch file_{1..3}{a..c}
rm file_{1..3}{a..c}
Checkpoint
1. Use the touch command and brace expansion to create files named
image_N.extension where:
2. Use the rm command to remove every file you just created with a single
command.
Parameter Expansion
Parameter expansion allows us to retrieve and modify stored values. The
symbol for this is ${...}.
To do this, we:
- set a parameter to a value, then
- use the dollar sign with the parameter name to get it later.
We can retrieve the value of the parameter with a dollar sign $ and the
name of the parameter.
echo $book
For example, echo ${book:6} returns the parameter’s value starting with
the sixth character in the string.
echo ${book:6}
Or echo ${book:6:3} will retrieve the value starting at the sixth character
and going for three characters.
Pattern Substitution
echo ${variableName/oldWord/newWord}
echo ${book/Ends/Begins}
In Pattern Substitution, one slash after the parameter replaces the first
instance of a match, and two slashes replaces them all.
Let’s replace all e’s with ampersands & using two forward slashes //.
echo ${book//e/&}
echo ${book/e/&}
Be sure to use the braces for most parameter expansions. If we left them
off, the shell will read the part after the parameter name as characters and
show them instead of using them to achieve what we wanted.
echo $book/e/$
Checkpoint
2. Use echo and parameter expansion to replace all of the a’s with an
underscore _.
Arithmetic Expansion
Bash can perform arithmetic expansion and then use the result in a
command. This is represented by a dollar sign and two parentheses
$((...)) containing a mathematical expression.
Bash previously used a dollar sign surrounded by single brackets, but this
is now obsolete, though may be present in older scripts.
echo $(( 4 + 5 ))
echo $(( 10 - 5 ))
echo $(( 6 * 3 ))
echo $(( 9 / 10 ))
important
Integers Only!
Bash can only work with integers. $ 9 / 10 = 0.9$, which is less than .
Since Bash can only handle integers, this operation gives us a result of
zero.
Checkpoint
1. plus multiplied by .
2. minus divided by .
3. divided by .
Challenge 3
4. Use the touch command and brace expansion to create files inside
each directory named image_N where:
image_1
image_2
5. Use the rm command to remove every file you just created with a single
command.
6. Now use the rmdir command to remove every directory we’ve created
with a single command.
When you are finished writing your script, click the Check
It! button to check your work.
Wild Card Characters
In Linux, a wild card is a symbol or group of symbols that replace other
characters. It can replace any character in a string. These are helpful for
finding texts that are similar in some way, but not the same.
ls *.txt
ls d*
Checkpoint
Bash uses a sub shell to run the specified command and the output is
returned to the current command.
It’s typically used with string manipulation tools to extract data from a
command’s output that has to be passed back up to the parent command.
.guides/img/commandSub
We could use uname -r to find the kernel release version and use command
substitution in an echo statement to display this information to the user.
Checkpoint
Globs and regular expressions are very similar. Globs specifically match
file names, where regular expressions match text.
ls *.txt
ls c*.txt
ls doc1?.txt
ls doc??.txt
ls doc[1-5].png
ls doc[1-5][a-e].png
ls doc[[:digit:]]a.txt
ls doc[[:digit:]][[:lower:]].txt
ls {*.txt,*.png}
Checkpoint
5. Click the Try It! button to run your script in the terminal and
check your output. It should look similar to the output below.
When you are finished writing your script, click the Check
It! button to check your work.