A Bash Primer
Command execution
The PATH
Be careful about including the
.
directory, representing the current working directory inPATH
. While this might seem convenient because it saves you from prefixing commands with./
, it creates a security risk. When your current working directory contains files that you didn’t create yourself, for example ones you downloaded, local files named after system commands are likely to take precedence. You might end up executing anls
command that does much more dangerous things than the original!
Command Output
Shell Variables
Positional Parameters
Command Input
Conditional execution
Loops
Shell Functions
Pattern matching
aka Globbing
Time-saving shell tools
Brace expansion
The curly brace expansion is a feature in the Bash shell that allows users to generate multiple strings with a single command by defining a range of values or a set of options within curly braces {}
. When the curly brace expansion is performed, the shell creates a separate string for each possible combination of the options or range of values defined within the braces. This can be used, for example, to quickly create multiple directories or files with similar names, or to generate a list of commands with different arguments. The resulting strings can then be used as arguments to other commands or stored in variables for later use.
Let’s say you want to create a set of directories for different months of the year. Instead of creating them one by one, you can use curly brace expansion to create all the directories at once. Here’s an example:
mkdir {Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec}_2023
When you run this command, the shell will expand the curly braces and create 12 directories, each with a name that combines the month abbreviation and the year 2023. The resulting directories will be:
Jan_2023 Feb_2023 Mar_2023 Apr_2023 May_2023 Jun_2023
Jul_2023 Aug_2023 Sep_2023 Oct_2023 Nov_2023 Dec_2023
The shell can also expand a range of numbers or letters. Let’s say you want to create a set of files with names that follow a specific pattern, such as “file1.txt”, “file2.txt”, and so on. You can use curly brace expansion with a range to quickly generate these file names. Here’s an example:
touch file{1..5}.txt
When you run this command, the shell will expand the curly braces and create 5 files with names file1.txt
, file2.txt
, file3.txt
, file4.txt
, and file5.txt
.
Elements in the expansion list may even be empty, which allows for a neat trick! Renaming files can be a tedious task when you have to repeat a complicated file name with just a slight alteration. Take this example:
mv Invoice-9388732.pdf Invoice-9388732.pdf.bak
If there are many similarly named files, even tab completion doesn’t make things much easier because you have to be cautious to pick the right alternative. Curly brace expansion to the rescue!
mv Invoice-9388732.pdf{,.bak}
This command expands the argument after mv
to two file names, one with a suffix of nothing, i.e. just the file name, and the second with .bak
added to its end.