Contact Us

Advanced BASH Exercises

 January 01, 2016
 J. David Giese
SHARE ON

BASH is the most widely-used and widely-supported shell for Linux. There are other shells that are better than BASH in various ways, but we feel that none of these other shells are better enough to warrant replacing BASH as the de-facto standard when writing user shell scripts. NetBSD and Debian use a stripped-down shell as the default for system scripts, which start with #!/bin/sh. BASH is preferred for user scripts, which start with #! /usr/bin/env bash. BASH is installed by default on almost all Unix-based operating systems, and the majority of the world’s shell scripts are written in BASH. For this reason, we suggest that all of our developers learn standard shell, starting with BASH.

BASH scripts are a domain-specific programming language that is well-suited to managing processes and files. That being said, the large number of special characters appropriated for process management, its text expansions, and its unusual syntax make BASH poorly-suited for general purpose programming. Accordingly, we think that BASH should only be used for scripts that are predominantly concerned with processes and files.

BASH is designed to be fast and convenient, and many commands must be typed frequently into the terminal. Thus, the design goals for BASH scripts (being terse and convenient) clash with those for code written in a general-purpose language (being general and readable). You can read some more discussion about this topic in our article on programming languages. It is worth keeping these design constraints in mind when learning BASH and writing scripts.

In this post, we provide a set of exercises that should help you solidify your knowledge of BASH. Note that these are NOT introductory level questions, and they assume that you are starting with a working knowledge of Linux and BASH. The questions focus on features of BASH that

  • Commonly cause confusion
  • Are very useful to know
  • Are commonly encountered when reading or writing simple BASH scripts.

Background reading 🔗

Before attempting to answer the questions, we recommend that you read the following resources:

Problems 🔗

To learn as much as possible from these exercises, write your responses before revealing the provided answers. If any exercises seem irrelevant, you can skip them and instead write a justification as to why they are unimportant. These justifications will help us improve the lesson for future employees.

Exercise 1

What is the difference between the .bashrc, .bash_profile, and .profile files?

Answer

Exercise 2

What is the difference between an environment variable and a variable and how are they related?

Answer

Exercise 3

What is a builtin command? Which of the following commands are builtins: cd, bash, echo, ls, exit, kill, [, [[? Why is it useful to know which commands are builtins?

Answer

Exercise 4

Explain in detail how BASH determines what to execute when you run a simple command, such as find . -name '*.py'.

Answer

Exercise 5

Briefly explain how BASH uses the PS1 and PS2 variables, and why they are useful.

Answer

Exercise 6

What is readline? What is the name of the file that allows you to customize its functionality?

Answer

Exercise 7

What is the difference between & and &&, | and ||?

Answer

Exercise 8

Is there a simple way to exclude a command from your BASH history (e.g. because you don’t want a password in your history file)?

Answer

Exercise 9

Explain what the following exit statuses mean to BASH: 0, 3, 103, 203, and 303.

Answer

Exercise 10

What is wrong with the command git commit -m "Make `SomeClass` auditable", and what is a simple way to fix it?

Answer

Exercise 11

Briefly describe what a signal and a trap is, and what the relevant the shell commands are to manipulate them.

Answer

Exercise 12

How many processes are created by the shell when you execute: a | b | c && d || e | f? Explain your reasoning in detail.

Answer

Exercise 13

What happens if you type “CTRL-Z” while waiting for the above command to complete?

Exercise 14

What happens to a background process when BASH closes?

Exercise 15

What is the difference between BASH options and shell options? Why Explain how you can view, set, and unset shell options? BASH options?

Exercise 16

What do the following shell options do: autocd, cdspell, cmdhist, globstar, histappend

Exercise 17

When is the BASH history written, and how can you manually tell it to update? (This can be useful when you want to have access to command history in another BASH session.

Exercise 18

What does set -e do, and why is it often a good idea to include in shell scripts?

Answer

Exercise 19

Describe the purpose of BASH’s expansion rules. List all the kinds of expansion BASH performs before executing a command, in order of how they are performed, and provide an example demonstrating the text before and after the expansion.

Answer
SHARE ON
×

Get To Market Faster

Weekly Medtech Tips

Our weekly Medtech tips will help you get safe and effective Medtech software on the market quickly. We cover regulatory process, AI/ML, software, cybersecurity, interoperability and more.