Escaping Characters in Bash on Linux


Abstract

Characters are used in source code, command lines, and the majority of computer interaction. The majority of characters, on the other hand, are not represented by keys on a standard keyboard, and many are not even readable. Another category of characters is complicated control characters.

We'll talk about character escape in Bash in this tutorial. We'll start off by briefly outlining how computers represent characters. First, we examine the various string types in Bash. The character escape in only Bash is then thoroughly described.

Note − Linux commands are case-sensitive.

Strings in Bash

Since a key does not represent each sign on a keyboard, writing and storing characters are two distinct processes. Strings are used in Bash to store text. Indeed, all variables in Bash are essentially character strings. Usually, they consist of single- or double-quoted, straight sequences.

We interpret or extrapolate particular character combinations in one context and take them literally in another, a crucial distinction between these two approaches.

Single Quotes

The primary goal of utilizing single quotes in Bash (') is to maintain each character's literal value. When you want the input to preserve its literal value without any data interpolation, use single quotes to extract any special value from a character. When you don't want to utilize the escape characters to alter how Bash understands the input string, single quotes are a useful alternative.

$ text='a $(echo b) c'
$ echo "${text}"
a $(echo b) c

Take note of how the single quotes keep the content intact. No interpolation is performed, but as a result, we are also unable to have a single quotation placed inside another single quote at any time.

Double Quotes

When history expansion is allowed, double quotes (") maintain the literal value of all characters with the exception of ($), ('), ("), (), and the (!) character.

$ text="a"
$ text="${text} $(echo "b") c"
$ echo "${text}"
a b c

Initially, we use the [a] key alone to directly assign the character to text. Next, using the ${text} variable expansion, we obtain its value. The $ are translated in this sentence. Lastly, we assign back to text by joining this value to an expression and still another character.

Escaping Characters in Bash

Characters in Bash having specific meanings must be escaped outside of single quotes in order to maintain their literal values. In actuality, this is mostly accomplished using the backslash escape character. Sometimes we may need to use alternative techniques.

Let's examine when and how each approach is used.

1.Double Quotes

A backslash character () inside of double quotes only has a specific significance when it is followed by ($), ('), ("), (), or a newline character. The backslash does not show up in the output if any of those characters are used after it within double quotes. If a backslash comes before it, a double quotation is acceptable between double quotes. To escape text inside double-quoted strings, we add the backslash character before the character.

$ text1="a $(echo b) c"
$ text2="a \$(echo b) c"
$ echo "${text1}"
a b c
$ echo "${text2}"
a $(echo b) c

In the above example, See how the dollar symbol is escaped in text2 so that it retains its literal meaning while losing its special functions

No Quotes

There is a cost associated with doing away with the quotes, as we have already demonstrated. The following characters must all be escapes in order for any sequence without quotes to be unified: comma, period, underscore, plus sign, colon, commercial at, percent sign, slash, and hyphen.

$ text=a\ \&\ b\ \&\ c
$ echo "${text}"
a & b & c

As a good practice, it is better not to use quotes.

Conclusion

We spoke about character escape in Bash in this tutorial. The various character encoding tables were the first thing we discovered. Also, we observed that some characters are only used as command or marker text and cannot be printed. We need a way to get away from such characters in order to use them literally. We looked at pure Bash as well as several typical built-in characters escaping situations in Bash.

This tutorial demonstrated how to use both single and double quotes in bash and clarified the differences between them. You should be able to tell the shell how to read the input strings and whether to output the input as is or to interpolate data once you have learned the distinctions.

Updated on: 04-Apr-2023

148 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements