Module #02 - Serious CLI Usage
This module will pick up where the previous one left as we will dig a bit deeper into the Linux CLI. The Bash shell is a complete programming language. Once you get used to interacting with an operating system via the command line, you generally discover a wealth of possibilities in terms of automating complex and, at times, boring tasks.
We will eventually talk about scripting in this course but, before we get there, we will start by getting familiar with the Bash shell variables, options, and various configurations possibilities. First, our modifications will be transient; i.e. only applying to the current Bash session with which we are experimenting. By the end of the module, we will learn how to make such modifications "stick around" from shell session to shell session and even make them apply to all users on our system.
Topics Studied in this Module
- T1 - Globbing
- Bash allows you to use so-called meta-characters to build expressions allowing you to designate sets of filenames or folder names on which you may apply all sorts of CLI tools.
- T2 - Shell Quoting & Escaping
- One of the most interesting topics when learning Bash; the syntax allowing you to control the interpretation of the above-mentioned meta-characters or even substitute the result of executing code in an expression.
- T3 - Bash Environment, Variables, & Option
- We then examine Bash options & variables.
- T4 - Bash Initialization Files
- Last but not least, we are going to take a look at how we may configure the Bash shell for your user accounts. We will consider individual configuration files first, then system-wide ones.
Learning Activities
This module features the following learning activities; DF, PA, GQ
Linux+ Exams Objectives
This module deals with material related to the following objectives from the CompTIA Linux+ and LPI certifications exams.
- Exam LX0-101 - Objective 103.1 - Work on the command line
- Exam LX0-101 - Objective 103.3 - Perform basic file management
- Exam LX0-102 - Objective 105.1 - Customize & use the shell environment
T1 - Globbing
The first topic we are going to study is related to one of the expansions the Bash shell applies to the commands you type in order to interpret special characters, also known as meta-characters, that you might have placed in them. More specifically, we are going to look into globbing, the mechanism that allows you to specify multiple file or folder names in a concise expression such as "*.txt" to mean "all files with suffic ".txt".
- Reading Assignments
- URL Link: Ryan Chadwick's Linux Tutorial
- The following sections of the Tutorial are assigned for this module;
T2 - Shell Quoting & Escaping
Next topic is related to a very interesting aspect of interacting with the Bash shell; understanding how to neutralize the special meaning of meta-characters when we want to make sure a '*' is just used by the shell as the symbol star without further expansions.
- Bash Meta-Characters and Backslash Escaping
- Video Link: YouTube
- Our first video will take a look at the available meta-characters and the simplest solution to neutralize their special meaning: the backslash.
- Bash Weak Quoting
- Video Link: YouTube
- We are now going to look at the so-called "weak-quoting" syntax in Bash.
- Bash Strong Quoting
- Video Link: YouTube
- Last but not least, we have one more quoting syntax to consider; the so-called "strong-quoting".
Now that we have covered the basics of quoting in Bash, we are going to look at a topic that is related, at least from the syntactical perspective, yet different. We want to specify parts of our command line that need to be executed and replaced by their result.
- Bash Command Substitution
- Video Link: YouTube
- We go over the two available syntaxes for command substitution in Bash. Make sure you understand the limits of the deprecated one.
T3 - Bash Environment, Variables, & Option
We are now ready to jump into how variables are handled in Bash.
- Bash Variables
- Video Link: YouTube
- First step is learning how to define, assign, initialize variables. We will then look at the two available syntaxes to access the value stored in a variable. After these basics, we will learn how to export variables and start our discussion of Bash environment and the two types of variables we are able to manage.
- Bash Environment
- Video Link: YouTube
- More information about the Bash environment.
- Bash Options
- Video Link: YouTube
- We finish our talk on the Bash environment with ways to list all variables therein. We then explore the various roles of the set command and how to use it to display and toggle Bash options.
T4 - Bash Initialization Files
Our next topic will focus on the initialization files used by the Bash command line shell when it is started. The video will give you an overview of the various configuration files that are involved as well as their contents.
- Bash Initialization Files
- Video Link: YouTube
- An overview of the various user-specific & system-wide initialization files executed when you start a Bash shell. The video also explains the differences between login vs. non-login shells and how to start each of them in order to test hands-on what the Bash manual page tells us.
To supplement the above-video, make sure to also study the relevant sections of the Bash manpage.
- Man Page - Bash
- File Link: manpages.ubuntu.com
- The above-video references the "INVOCATION" section in the Bash man page for information about the various initialization files used when starting a Bash shell.
This Discussion Forum assignment will allow us to further our previous initial exploration of what open source means. The format we will adopt will be that of a pro vs. cons discussion.
Topic - Deciding whether to go Open Source or not
The idea of this DF is to have you discuss with one another the pro and cons of adopting open source solutions. To this end, you will have to put yourself in the shoes of an IT professional working in one of the following sectors;
- Private Corporations (non IT)
- Private Corporations (IT related)
- Non-profit Organizations
- Military Organizations
- Scientific & Research Institutions
- Educational Organizations
- Governmental Organizations
Make sure that the title of your post indicates which of the above sectors you selected.
Requirements for your Posts
You will have to make two posts on this module's DF forum.
Your first post will state arguments in favor, or against, the adoption of open source software from the perspective of one of the above-listed sectors.
After you make this first post on the DF forum, you will be able to see the posts made by other students. Pick one and respond to the arguments that were developed in it. You may either agree and provide new arguments or references to new resources that further establish the points being made, or disagree and provide similar material contesting the points being made.
How to get a good DF grade
If everyone waits late in the week, e.g. thursday, to make their first post, this assignment will end up rushed and mostly frustrating for those who wanted a bit of time to formulate their responses to their peers. Post late and you will lose points, potentially all.
Opinions are easy to come by, try to Google anything subject to controversy and you will find thousands of them. As such, their value is nil, except if they are backed by facts and logically articulated arguments. Make sure that both in your original post, and your response, you reference verifiable information that you found in order to support the points you are making. Students who simply post arbitrary opinions will not receive any points in this kind of DF.
This module's PA will use short and simple exercises to help you practice hands-on the various syntaxes we discussed in the study material. For each exercise, we will provide you with details on what you need to submit in order to receive credit. Make sure that you collect all required information in a single document that you will then upload as a PDF file.
Exercise #1 - Setting Variables
Let us start by practicing our skills in creating different types of variables in the Bash shell;
- Create a local shell variable mylocalvar and assign to it the string value "This is local"
- Create a global shell variable, also known as environment variable, MYENVVAR and assign to it the value "This is in the environment"
For this exercise, submit a screenshot of your command line terminal that shows your full name the contents of both variables, as well as the commands you used to set them.
Exercise #2 - Verifying the Nature of these Variables
While the above exercise allowed us to verify that the proper value was assigned to each variable, we did not do anything to ensure they really were local and global, respectively.
To do so we are going to cheat a bit and introduce syntax that we will fully study in a later module. The study material mentioned both printenv (or env) and set as ways to list local shell variables, global environment variables, and also a whole lot more of information we do not need right now.
We are going to use these commands but follow them with a bit of syntax that we will study only in a later module. The result will be to filter out every line from the output of these commands that does not refer to the variables we are interested in looking up.
This is what we get when filtering for both variables while using the previoulsy mentioned commands;
tux@tuxbox$ printenv | grep mylocalvar tux@tuxbox$ printenv | grep MYGLOBALVAR MYGLOBALVAR=This is global tux@tuxbox$ set | grep mylocalvar mylocalvar='This is local' tux@tuxbox$ set | grep MYGLOBALVAR MYGLOBALVAR='This is global'
This tells us that printenv shows no output related to mylocalvar but only MYGLOBALVAR. Makes sense. But then, set shows output for both variables even though one is local and the other global. Why is that so?
For this exercise, submit a screenshot of your command line terminal that shows your full name, the output when you run the commands shown in the above sample shell output. In addition, provide a brief explanation of why set shows output for both variables even though one is local and the other is global.
Exercise #3 - Common Bash Environment Variables
How would you go about finding out which Bash environment variables contain the following information and then display their contents using echo?
- The full path of the executable program being used as shell in this terminal
- The type of terminal being used in the console you are typing
- Your host name
- The type of architecture of your host; e.g. x86_64, amd64...
- The name of the user as which you are logged
For this exercise, submit a screenshot which displays the environment variables that contain the following information;
- The full path of the executable being used as the shell in your terminal
- The type of terminal being used in the console you are typing.
- Your host name
- The type of architecture of your host (x86_64, amd64, etc).
- The name of the user which you are logged in as.
Exercise #4 - The Path is Closed
The PATH variable was mentioned in the study material. For this exercise, do a little bit of research on the CDPATH variable and explain, in your own words, how it is similar, as well as how it differs, from the PATH variable. In addition, you will also describe a scenario for which you would use CDPATH.
Exercise #5 - Bash Quoting
Provide the echo command you would type in your shell to display each of the following outputs;
The dog & the cat; a tale of getting along just fine I remember now... "Bash is fun", they said. Liars!To do so, you will provide solutions that only use the backslash character, and then solutions that only use the single quote character.
Try to do the same with the following outputs. Why isn't it working?
It's rather annoying (and even at times infuriating) to see quoting fail
Exercise #6 - Command Substitution
The commands whoami and date are quite handy. We want to display with a single echo command the following message;
Hello, tux today is Tue May 8 17:20:40 EDT 2018 what do you want to do?
Obviously, we want "tux" to be replaced by the name of the user you are logged as, and the date by the current date of the system.
There are two syntaxes that will allow to specify what part of an echo statement you want the shell to actually execute and substitue by its output. Use both to achieve the above goal.
Exercise #7 - While we are at it...
The above message would be a nice personalized prompt for a shell. How would we go about telling our current shell to use it as such for the rest of the session?
Do you notice something strange about the so-called current date that is now being displayed in your prompt for each line?
How would you fix this? This question is tricky. Do not simply try things at random and make sure you understand why things are working, or not.
Exercise #8 - More Bash Quoting
The double quotes are used by Bash to allow the interpretation of some special characters while blocking others. There are actually only a few meta-characters that are still interpreted as such when encased in double quotes. Which ones are these?
Now that you remembered them, or looked them up, come up with a single output that you would want a candidate at a job interview to be able to echo. Your challenge must require them to use all three meta characters. Document in your PA answer what is the echo statement the candidate would have to come up with in order to display the output you crafted.
Exercise #9 - Hacker Wannabe
It is a good day for a prank. A friend left their workstation unlocked with a terminal running bash opened. We want to make it so that, when they use ls, the following message is displayed instead of running the command;
tux@tuxbox$ ls ERROR 0xFF42 (bad sector, data corrupted). Contact NSA for a backup copy of your data. tux@tuxbox$ ls
How would you go about doing this?
You should definitively take a snapshot of your machine BEFORE you make any modifications to your system for this exercise. This way, you will be able to easily revert back to a working state if you mess up something.
This tab provides you with optional resources meant for those who want to learn a bit more about the topics covered in this module.
Resources on the Bash shell
Wooledge.org tutorials;
The following website was recommended by one of our students, Issam Halabi, during summer 2019. It takes a gamified approach to guiding your first steps on the Linux Command Line; OverTheWire: Bandit.
Machtelt Garrels' Bash Beginners Guide
If you are interested in getting a bit deeper into Bash scripting, we recommend you study Machtelt Garrels' Bash Beginners Guide. The following links will provide you with a local PDF copy and the URL of the website where you might find the latest version. Please note that this document is freely available as part of the TLDP - The Linux Documentation Project.
- Resource - Bash Beginners Guide
- File Link: Bash-Beginners-Guide.pdf URL: TLDP website
- The following sections of the Bash Beginners Guide are relevant readings for this module.
We will have an opportunity to dig into other aspects of the material presented in that guide when we study Bash scripting in a later module.
By the time we are done with this course, you should be able to read that guide without problems. The following sections are the ones directly relevant to this module;
Harley Hahn's Guide to UNIX and Linux
If you are interested in learning more about the topics covered in this module, I recommend you look for Harley Hahn's Guide to UNIX and Linux. It is unfortunately out of print but is a great (and entertaining) read about the topic.
The following sections of the above are particularly relevant to this module;
- Section 11: The Shell
- Section 12: Using the Shell: Variables & Options
- Section 13: Using the Shell: Commands & Customization