Posts How to deal with escape characters in Linux?
Post
Cancel

How to deal with escape characters in Linux?

Quoting

In this blog let’see abaout escape handling in Linux commands

Scenario

To preserve whats’ been passed and not to convert / expand / modify and take it as literal.

Eg: You may need to print the variable name $abc and this should not be referred to it’s value stored

How can we instruct linux to take it as litereal?

Above can be achieved with 3 different methods

Method 1 - Backslash

Use Backslash \ without quotes to take the next single character as literal

Example - without backslash

1
2
3
➜  ~ abc=test\hi
➜  ~ echo $abc
testhi

In above \ is used to treat next character h as literal hence \ is not printed. Example - with backslash

1
2
3
➜  ~ abc=test\\hi
➜  ~ echo $abc
test\hi

In above first slash enables to intstruct second slash as literal hence once slash is printed.

Method 2 - Doublequotes

You can use Double quotes to treat everycharacter as literal including backslash except $ for variable name. ie) variable name will be convered to value

Example:

1
2
3
➜  ~ abc=test
➜  ~ echo "Hi \ $abc"
Hi \ test

In above you could see variable $abc is replaced with it’s value and \been treated as literal.

Method 3 - Singlequotes

Single quotes is used to treat everything literally. including any variable

Example:

1
2
3
➜  ~ abc=test
➜  ~ echo 'Hi \ $abc'
Hi \ $abc

Hope this explains when to use ` and “ in your scenario.


Thanks for reading till end. For any queries you can message us in insta @techpechu or mail to : techpechu@gmail.com

We do run a youtube channel in Tamil called TechPechu. Do subscribe for moral support. Happy Learning.

Follow us on instagram @techpechu