1. Introduction

After storage has become more or less a non-issue, text files have become the building blocks of configuration and even many data formats. Because of this, knowing how to process a text file quickly can be paramount to the administration and proper functioning of a system. Although it may seem like an easy task at first, some potentially special characters, like the backslash, can make it less so.

In this tutorial, we’ll talk about backslashes and how to remove them from text files and strings.

We tested the code in this tutorial on Debian 11 (Bullseye) with GNU Bash 5.1.4. It should work in most POSIX-compliant environments unless otherwise specified.

2. The Backslash

When dealing with special symbols, control sequences, and others, the *\* backslash is the standard escape character.

In fact, **since the *\* backslash itself is a special symbol, we usually need to escape it like *\\***. Otherwise, we’d only escape the following character, which may result in a special character or the backslash just being ignored or left in.

2.1. Character Notation and Sequences

To begin with, character notations with minor exceptions mostly use backslash as the start of their escape sequences:

  • \x07 – hexadecimal escape
  • \007 – octal escape
  • \a – ANSI escape

Above are three ways to output the non-printable alarm bell character. Escaping any one of them with another backslash produces a simple string comprising all escape sequence characters.

2.2. Interactive Shell Symbols

For convenience, interactive shells have shortcuts and shorthands like the ! exclamation point operator for common tasks:

$ echo !event
-bash: !event: event not found
$ echo \!nonevent
!nonevent

Here, we use the echo command to output a string that starts with an ! exclamation point. If we don’t escape this operator with a backslash, Bash treats the expression as an event with the !pattern syntax that runs a previous command.

2.3. Non-interactive Shell Symbols

While the exclamation point works as we saw only in interactive shells, escaping may be needed for other special characters in most situations:

$ echo ~
/home/baeldung
$ echo \~
~

In this case, we see the ~ symbol that expands to $HOME showing the user’s home folder. However, that happens only if we don’t escape it.

2.4. Shell Strings and Quotes

When it comes to shell string quotes, the backslash is treated differently, depending on the presence and kind of quotes:

$ echo \\
\
$ echo "\\"
\
$ echo '\\'
\\

Evidently, two *\\* backslashes without any quoting or within double quotes result in a single actual backslash. Meanwhile, ‘\\’ means two backslashes.

In fact, while single quotes preserve the raw meaning of any character, except the single quote, only four characters make the backslash an escape character within double quotes:

  • $ dollar sign
  • ` backtick
  • double quote
  • *\* backslash

Still, commands like echo (with -e) and printf can also understand ANSI escape codes, which is not part of the native Bash string interpretation.

2.5. Regular Expressions

*The special functions of the \* backslash apply to any regular expression (regex) syntax as well:

$ [[ "te\\st" =~ te\\st ]] && echo 'Match.'
Match.
$ [[ 'te\\st' =~ te\\\\st ]] && echo 'Match.'
Match.

Here, **the actual \\* double backslashes within single quotes match \\\\* two escaped backslashes in the regex.

In fact, when it comes to Perl Compatible Regular Expressions (PCRE), there are special meanings to some backslash-escaped characters:

$ perl -e 'print "Match." if "te st" =~ /te\sst/'
Match.

In this perl -e one-liner, \s means any whitespace character.

However, in non-PCRE regex flavors, we can use a single backslash within a character group like [\] as long as it’s not followed by a character that makes it an ANSI sequence.

2.6. Terminal Control

When it comes to terminals, we can use a backslash to start a Control Sequence Introducer (CSI), i.e., an \e ASCII ESC character followed by a left square bracket:

$ echo "B\e[1DBaeldung."
B\e[1DBaeldung.
$ echo -e "B\e[1DBaeldung."
Baeldung.

Here, we use the \e[1D sequence, which moves the cursor a character to the left, effectively overwriting the first B with the second. Importantly, we had to add the -e flag to echo to interpret the sequences.

Moreover, we can apply text style formatting and perform other actions via the same controls.

3. Remove All Backslashes

Sometimes, we might want to ensure no special escaping takes place due to the contents of a given file or string. In such instances, we can strip the *\* backslash characters altogether.

In cases involving commands without special switches, we can pipe the modifications through tee to store them back to a different or the same file in place:

$ cat file | [modification commands] | tee file

Of course, we can replace the cat command with any string-producing command and skip the file writeback. For clarity, we use both options in most examples below. Now, let’s see different ways to remove the backslash.

3.1. Using Bash

When using Bash alone, we can pipe to a while loop with read:

$ cat file | while read line; do echo ${line//\\/}; done | tee file

Here, we process each line via the ${VAR//REGEXP/REP/} Bash regexp syntax. Notably, we escape the backslash.

3.2. Using tr

Of course, we can use the tr command to–delete (-d) a specific character:

$ cat file | tr --delete '\\' | tee file

Critically, even in single quotes, tr expects us to escape the backslash.

3.3. Using sed

Naturally, we can use sed to modify the input content. In fact, we can also employ the –in-place or -i flag instead of tee:

$ sed --in-place 's/\\//g' file

Alternatively, we can always pipe in the data:

$ cat file | sed 's/\\//g'

Here, we don’t save our changes back to a file.

3.4. Using AWK

The standard awk command can also remove backslashes from a given dataset:

$ cat file | awk '{gsub(/\\/,""); print}' | tee file

In this case, we use the gsub() function to replace all backslashes on each line globally and then print it.

3.5. Using Perl

Since regular expressions are more or less native to Perl, using the interpreter for our needs is practical:

$ cat file | perl -pe 's/\\//g;' | tee file

With perl, we just add the -p flag to ensure each line is printed and apply the same substitution syntax that we used with sed earlier.

In fact, Perl can get the file as an argument and provides an edit [-i]n-place flag as well:

$ perl -i -pe 's/\\//g;' file

This command is equivalent to the earlier catperltee pipeline.

4. Summary

In this article, we talked about the backslash character and how to remove it from a file or string.

In conclusion, there are many ways to remove backslashes but we should be aware of their special function within the context of the content we edit.