Published on November 25th 2024 by Felix Halim
If you’re working with Bash command-line interface, you’re likely to come across a variety of metacharacters that can make your life easier. In this article, we’ll explore some of the most useful metacharacters in Bash and provide examples for each of them.
Let’s start by reviewing some of the metacharacters we’ve already covered in previous article:
Metacharacter | Description |
---|---|
< | Input redirection |
> | Output redirection |
>> | Output redirection (append) |
; | Command separator |
/ | Directory separator |
! | History expansion |
These metacharacters are commonly used in Bash and can help you perform a variety of tasks, such as redirecting input and output, piping commands, and separating commands.
But there are many other metacharacters in Bash that can be equally useful. Let’s take a closer look at some of them.
Pattern matching is a powerful feature in Bash that allows you to match files and directories based on specific patterns. Here are some of the most commonly used pattern matching metacharacters:
Pattern | Description |
---|---|
* | Zero or more characters |
? | Single character wildcard |
[..] | Filename pattern |
{.., ..} | Brace expansion |
Here are some examples of how you can use these metacharacters:
# Match all files with a .txt extension
felixhalim@learning-bytes:[~]$ ls *.txt
# Create files with names file0, file1, file2, ..., file10
felixhalim@learning-bytes:[~]$ touch file{0..10}
# Match all files with a name starting with "file" and ending with a number
felixhalim@learning-bytes:[~]$ ls file[0-9]*
file0 file1 file10 file2 file3 file4 file5 file6 file7 file8 file9
# Create files with names fileA, fileB, fileC, ..., fileZ
felixhalim@learning-bytes:[~]$ touch file{A..Z}
# Match all files with a name starting with "file" and ending with a letter
felixhalim@learning-bytes:[~]$ ls file[[:alpha:]]*
fileA fileC fileE fileG fileI fileK fileM fileO fileQ fileS fileU fileW fileY
fileB fileD fileF fileH fileJ fileL fileN fileP fileR fileT fileV fileX fileZ
# Match all files with a name starting with "file" and ending with either a number or a letter
felixhalim@learning-bytes:[~]$ ls file[[:alnum:]]*
file0 file2 file5 file8 fileB fileE fileH fileK fileN fileQ fileT fileW fileZ
file1 file3 file6 file9 fileC fileF fileI fileL fileO fileR fileU fileX
file10 file4 file7 fileA fileD fileG fileJ fileM fileP fileS fileV fileY
# Match all files with a name starting with "file" and ending with two characters
felixhalim@learning-bytes:[~]$ ls file??
file10
# Remove all files with a name starting with "file"
felixhalim@learning-bytes:[~]$ rm file*
felixhalim@learning-bytes:[~]$ echo gic-{tg,dsg,iig}
gic-tg gic-dsg gic-iig
felixhalim@learning-bytes:[~]$ echo {a..f}-profile
a-profile b-profile c-profile d-profile e-profile f-profile
felixhalim@learning-bytes:[~]$ echo rank-{1..5}
rank-1 rank-2 rank-3 rank-4 rank-5
If you are constructing strings, use a brace expansion. If you are matching filenames, use a filename pattern.
Metacharacter | Description |
---|---|
\ | Escape special characters |
cmd |
Command substitution (old) |
$(cmd) | Command substitution |
# | Comments in Bash scripting |
& | Background processes |
$ | Variable expression |
’ | Quote mark (strong) |
” | Quote mark (weak) |
Escape characters are used to escape special characters in Bash. Here’s an example:
Metacharacter | Description |
---|---|
\ | Escape special characters |
# Match all files with a name containing a space
ls file\\ with\\ space.txt