Master Bash with These Must-Know Special Characters: Unlock the Secrets of the Command Line!

Master Bash with These Must-Know Special Characters: Unlock the Secrets of the Command Line!

Learn the meaning behind the mysterious symbols on the Bash command line Discover 15 essential special characters and how to effectively use them for enhanced command line navigation and manipulation

Key Takeaways

Special characters often function like command short-hand, and tell Bash to perform a specific function without having to type out a longer, more verbose command.

The tilde (~) symbol represents the home directory, double periods (..) indicate the parent directory, and a single period (.) represents the current directory.

Other essential characters used in directory-based operations include the forward slash (/) for directory separation, the hash symbol (#) for comments, the question mark (?) for wildcard characters, the semicolon (;) for separating commands, and the pipe symbol (|) for piping commands together.

If you want to become a hero of hieroglyphics and master the Bash shell on Linux, macOS, or another UNIX-like system, understanding and utilizing special characters (such as ~, *, |, and >) is crucial. We will guide you in deciphering these mysterious Linux command sequences.

What Are Special Characters?

The Bash shell treats a specific group of characters in two distinct ways. When entered into the shell, these characters function as instructions or commands, directing the shell to carry out a particular action. They can be thought of as one-character commands.

Occasionally, you may simply want to display a character without invoking its special functionality. Fortunately, there is a method to represent the character itself instead of its designated function.

We'll show you which characters are "special" or "meta-" characters, as well as how you can use them functionally and literally.

~ Home Directory

The tilde (~) symbolizes your home directory, eliminating the need to input the entire path when using commands. Regardless of your current location in the filesystem, you can simply use the following command to navigate to your home directory:

cd ~

Master Bash with These Must-Know Special Characters: Unlock the Secrets of the Command Line!

You can also utilize this command with relative paths. For instance, if you are currently located in a directory that is not within your home folder and wish to switch to the archive folder in your work directory, you can use the tilde symbol to accomplish this:

cd ~/work/archive

Master Bash with These Must-Know Special Characters: Unlock the Secrets of the Command Line!

. Current Directory

A period (.) represents the current directory. You see it in directory listings if you use the -a (all) option with ls.

ls -a

Master Bash with These Must-Know Special Characters: Unlock the Secrets of the Command Line!

You can also use the period in commands to represent the path to your current directory. For example, if you want to run a script from the current directory, you would call it like this:

./script.sh

This tells Bash to look in the current directory for the script.sh file. This way, it won't search the directories in your path for matching executable or script.

Master Bash with These Must-Know Special Characters: Unlock the Secrets of the Command Line!

.. Parent Directory

The double period or "double dot" (..) represents the parent directory of your current one. You can use this to move up one level in the directory tree.

cd ..

Master Bash with These Must-Know Special Characters: Unlock the Secrets of the Command Line!

You can also utilize this command using relative paths. For instance, if you wish to navigate up one level in the directory tree and then enter another directory at that level.

Additionally, you can employ this technique to swiftly move to a directory at the same level in the directory tree as your current one. You ascend one level and then descend back into a different directory.

cd ../gc_help

Master Bash with These Must-Know Special Characters: Unlock the Secrets of the Command Line!

/ Path Directory Separator

You can use a forward-slash (/)---often just called a slash---to separate the directories in a pathname.

ls ~/work/archive

Master Bash with These Must-Know Special Characters: Unlock the Secrets of the Command Line!

Use the command "cd /" to quickly navigate to the root directory in the Linux directory tree.

Master Bash with These Must-Know Special Characters: Unlock the Secrets of the Command Line!

# Comment or Trim Strings

The hash or number sign (#) is commonly used to indicate a comment in the shell, disregarding any action that follows. It is applicable in shell scripts and, to a lesser extent, on the command line.

# The Bash shell will disregard this comment.

Master Bash with These Must-Know Special Characters: Unlock the Secrets of the Command Line!

It isn't truly ignored, however, because it's added to your command history.

Using the hash symbol, you have the ability to trim a string variable and eliminate certain text from the start. By executing this command, a new string variable named "this_string" is formed.

To illustrate, we set the variable to contain the phrase "Dave Geek!".

this_string="Dave Geek!"

Master Bash with These Must-Know Special Characters: Unlock the Secrets of the Command Line!

The words "How-To" are printed to the terminal window using the echo command. The value stored in the string variable is retrieved through parameter expansion. The hash and the text "Dave" are appended, and this portion of the string is then removed before it is passed to echo.

Revised content:

Print the words "How-To" to the terminal window by echoing the value stored in the string variable using parameter expansion. Trim off the portion of the string that includes "Dave" by appending a hash and the text.

Master Bash with These Must-Know Special Characters: Unlock the Secrets of the Command Line!

The value stored in the string variable remains unchanged, only modifying what is displayed with echo. Let's verify this by echoing the value of the string variable again:

Master Bash with These Must-Know Special Characters: Unlock the Secrets of the Command Line!

echo $this_string

Master Bash with These Must-Know Special Characters: Unlock the Secrets of the Command Line!

? Single Character Bash Wildcard

The Bash shell possesses three wildcards, including the question mark (?). These wildcards are employed for substituting characters in filename templates. When a filename incorporates a wildcard, it establishes a template that qualifies a variety of filenames instead of just a single one.

The question mark wildcard represents exactly one character. Consider the following filename template:

ls badge?.txt

The files that match this criteria include those with numbers or letters after the "badge" section of the filename. The question mark wildcard will match any single character, whether it is a letter or a number.

Master Bash with These Must-Know Special Characters: Unlock the Secrets of the Command Line!

The "badge.txt" filename template is incorrect as it does not have any character between "badge" and the file extension. To make it valid, the question mark wildcard should match a corresponding character in the filename.

To discover text files with exactly five characters in their filenames, employ the question mark as follows:

Execute the command: ls ?????.txt

Master Bash with These Must-Know Special Characters: Unlock the Secrets of the Command Line!

* Character Sequence Bash Wildcard

You can use the asterisk (*) wildcard to stand for any sequence of characters, including no characters. Consider the following filename template:

ls badge*

This matches all of the following:

Master Bash with These Must-Know Special Characters: Unlock the Secrets of the Command Line!

It matches "badge.txt" because the wildcard represents any sequence of characters or no characters.

This command matches all files called "source," regardless of the file extension.

ls source.*

Master Bash with These Must-Know Special Characters: Unlock the Secrets of the Command Line!

[] Character Set Wildcard

As covered above, you use the question mark to represent any single character and the asterisk to represent any sequence of characters (including no characters).

By using square brackets ( [] ) in combination with specific characters, you can create a wildcard. Subsequently, the corresponding character in the filename should match with at least one of the characters present in the wildcard set.

In this instance, the command signifies: "any file having an extension of ".png", a filename that commences with "pipes_0," and the subsequent character could either be 2, 4, or 6."

ls badge_0[246].txt

Master Bash with These Must-Know Special Characters: Unlock the Secrets of the Command Line!

You can use more than one set of brackets per filename template:

ls badge_[01][789].txt

Master Bash with These Must-Know Special Characters: Unlock the Secrets of the Command Line!

You can also include ranges in the character set. The following command selects files with the numbers 21 to 25, and 31 to 35 in the filename.

ls badge_[23][1-5].txt

Master Bash with These Must-Know Special Characters: Unlock the Secrets of the Command Line!

; Shell Command Separator

You can type as many commands as you like on the command line, as long as you separate each of them with a semicolon (;). We'll do this in the following example:

ls > count.txt; wc -l count.txt; rm count.txt

Master Bash with These Must-Know Special Characters: Unlock the Secrets of the Command Line!

Content must be rewritten in a better way.

Please note that the second command will be executed regardless of whether the first command fails or not. Similarly, the third command will be executed regardless of whether the second command fails or not, and so on.

However, if you want the sequence of execution to stop if any of the commands fails, you should use a double ampersand (&&) instead of a semicolon:

cd ./doesntexist && cp ~/Documents/reports/* .

Master Bash with These Must-Know Special Characters: Unlock the Secrets of the Command Line!

& Background Process

Once a command is entered in the terminal window and successfully executed, you will be returned to the command prompt. Typically, this process is swift, lasting only a few moments. However, if you were to open another program, like gedit, your terminal window will remain inaccessible until the newly launched application is closed.

You can, however, launch an application as a background process and continue to use the terminal window. To do this, just add an ampersand to the command line:

gedit command_address.page &

Master Bash with These Must-Know Special Characters: Unlock the Secrets of the Command Line!

Bash shows you the process ID of what launched, and then returns you to the command line. You can then continue to use your terminal window.

< Input Redirection

Several Linux commands have the capability to accept a file as an argument and extract their data from that file. Additionally, these commands can also receive input from a stream. In order to create a stream, you can utilize the left-angle bracket ( < ) operator, as depicted in the following example, to redirect a file into a command:

sort < words.txt

Master Bash with These Must-Know Special Characters: Unlock the Secrets of the Command Line!

Redirecting input to a command can alter its behavior compared to reading from a specific file.

For instance, when using the wc command to count words, lines, and characters in a file, it typically displays the values followed by the filename. However, if we redirect the file's contents to wc, it will still output the numerical values but will not be aware of the original file's name, and therefore cannot include it in the output.

Here are some examples of how you can use wc:

wc words.txt

wc < words.txt

Master Bash with These Must-Know Special Characters: Unlock the Secrets of the Command Line!

> Output Redirection

You can use the right-angle bracket ( > ) to redirect the output from a command (typically, into a file); here's an example:

ls > files.txt

cat files.txt

Master Bash with These Must-Know Special Characters: Unlock the Secrets of the Command Line!

Output redirection can also redirect error messages if you use a digit (2, in our example) with >. Here's how to do it:

wc doesntexist.txt 2> errors.txt

cat errors.txt

Master Bash with These Must-Know Special Characters: Unlock the Secrets of the Command Line!

| Pipe

A "pipe" connects commands together, passing the output of one command as input to the next. The length of the chain can vary.

In this example, we will use the cat command to provide the contents of the words.txt file to grep. grep will extract any line that includes either a lowercase or uppercase "C." The lines extracted by grep will then be passed to sort. The sort command is configured with the -r option to display the sorted results in reverse order.

We typed the following:

cat words.txt | grep [cC] | sort -r

Master Bash with These Must-Know Special Characters: Unlock the Secrets of the Command Line!

! Pipeline logical NOT and History Operator

The exclamation point (!) is a logical operator that means NOT.

There are two commands in this command line:

[ ! -d ./backup ] && mkdir ./backup

The first command is the text within the square brackets;

The text following the double ampersands is referred to as the second command.

The first command utilizes the logical operator "!" and is enclosed in square brackets to indicate a test. The test is conducted to check for the existence of a directory named "backup". If the directory does not exist, the second command is executed to create it.

Using double ampersands in Bash allows the second command to be executed only if the first command is successful. However, this does not align with our requirements. If the test for the "backup" directory is successful, there is no need to create it. On the other hand, if the test fails for the "backup" directory, the second command will not be executed, resulting in the absence of the desired directory.

Here's where the logical operator ! comes into play. It functions as a logical NOT. Therefore, when the test succeeds (indicating the existence of the directory), the ! negates it to "NOT success," indicating failure. Consequently, the second command remains inactive.

If the test for the directory's existence fails, the response is changed to "NOT failure" using the ! symbol, indicating success. As a result, the command to create the missing directory is executed. This small but powerful ! symbol proves its significance in such situations!

To check the status of the backup folder, you use the ls command and the -l (long listing) and -d (directory) options, as shown below:

ls -l -d backup

Master Bash with These Must-Know Special Characters: Unlock the Secrets of the Command Line!

Additionally, you have the option to execute commands from your command history using the exclamation point. By utilizing the history command, you can view your command history and subsequently re-run a specific command by typing its corresponding number followed by the exclamation point to execute it. This feature is demonstrated in the example below:

!24

Master Bash with These Must-Know Special Characters: Unlock the Secrets of the Command Line!

The following re-runs the previous command:

!!

$ Variable Expressions

In the Bash shell, you can create variables to store values. Certain variables, like environment variables, are always present and can be accessed whenever you open a terminal window. These variables store information like your username, home directory, and path.

You can use echo to see the value a variable holds---just precede the variable name with the dollar sign ($), as shown below:

echo $USER

echo $HOME

echo $PATH

Master Bash with These Must-Know Special Characters: Unlock the Secrets of the Command Line!

In order to create a variable, you need to assign it a name and assign a value to it. It is not necessary to use the dollar sign when initializing a variable. The dollar sign is only used when referring to a variable, as demonstrated in the following example:

ThisDistro=Ubuntu

MyNumber=2001

echo $ThisDistro

echo $MyNumber

Master Bash with These Must-Know Special Characters: Unlock the Secrets of the Command Line!

Add braces ( {} ) around the dollar sign and perform a parameter expansion to obtain the value of the variable and allow further transformations of the value.

This creates a variable that holds a string of characters, as shown below:

MyString=123456qwerty

Use the following command to echo the string to the terminal window:

echo ${MyString}

To return the substring starting at position 6 of the whole string, use the following command (there's a zero-offset, so the first position is zero):

echo ${myString:6}

If you want to echo a substring that starts at position zero and contains the next six characters, use the following command:

echo ${myString:0:6}

Use the following command to echo a substring that starts at position four and contains the next four characters:

echo ${myString:4:4}

Master Bash with These Must-Know Special Characters: Unlock the Secrets of the Command Line!

Quoting Special Characters

To use a special character as a literal (non-special) character in the Bash shell, you need to apply quoting. There are three methods to accomplish this.

By enclosing the text in quotation marks ("..."), you prevent Bash from interpreting most special characters and instead display them as is. However, it is important to note that the dollar sign ($) retains its functionality as a variable expression, allowing you to include variable values in your output.

For example, this command prints the date and time:

echo "Today is $(date)"

If you enclose the text in single quotes ('...') as shown below, it stops the function of all the special characters:

echo 'Today is $(date)'

You can use a backslash ( \ ) to prevent the following character from functioning as a special character. This is called "escaping" the character; see the example below:

echo "Today is \$(date)"

Master Bash with These Must-Know Special Characters: Unlock the Secrets of the Command Line!

Memorizing special characters as concise commands can greatly enhance your comprehension of the Bash shell and facilitate understanding of scripts created by others.

Linux Commands

Files

tar · pv · cat · tac · chmod · grep · diff · sed · ar · man · pushd · popd · fsck · testdisk · seq · fd · pandoc · cd · $PATH · awk · join · jq · fold · uniq · journalctl · tail · stat · ls · fstab · echo · less · chgrp · chown · rev · look · strings · type · rename · zip · unzip · mount · umount · install · fdisk · mkfs · rm · rmdir · rsync · df · gpg · vi · nano · mkdir · du · ln · patch · convert · rclone · shred · srm · scp · gzip · chattr · cut · find · umask · wc · tr

Processes

alias · screen · top · nice · renice · progress · strace · systemd · tmux · chsh · history · at · batch · free · which · dmesg · chfn · usermod · ps · chroot · xargs · tty · pinky · lsof · vmstat · timeout · wall · yes · kill · sleep · sudo · su · time · groupadd · usermod · groups · lshw · shutdown · reboot · halt · poweroff · passwd · lscpu · crontab · date · bg · fg · pidof · nohup · pmap

Networking

netstat · ping · traceroute · ip · ss · whois · fail2ban · bmon · dig · finger · nmap · ftp · curl · wget · who · whoami · w · iptables · ssh-keygen · ufw · arping · firewalld