Everything Shell Script

  • Home
  • Home

Saturday, 8 October 2016

Escape character in Shell Script

 Niraj Bhagchandani     20:28:00     No comments   

Okay!! uptil now we have seen a lot of stuffs regarding Shell Script. But how about escape characters

Example: What if you want to display: Hi "Niraj" ?

So, if you type

sh-4.2 $ echo "Hi "Niraj"" 

The first and last " characters wrap the whole lot into one parameter passed to echo so that the spacing between the two word is kept as is.

But the above code would interpret as
  • "Hello "
  • World
  • ""
Thus, the output would be

Hello World

Please note here that we loose the double quotes entirely as they are not printed during this process.

Thus, if you want to display the quotes you are in great need of escape characters.

sh-4.2 $ echo "Hi \"Niraj\" "

Here the first Argument treated individual is
  •  " -> start of the string
  • Hi
  • \" is treated as "
  • Niraj
  • \" is again treated as "
  • " -> end of the string
 Now, Let us look for one more wildcards and understand.(Astrick *)

sh-4.2 $ echo *
first.sh  second.sh etc.... will list files in the current directory.
sh-4.2 $ echo *txt 
Will list all txt files in current directory
sh-4.2 $ echo "*" 
Will print * as a literal / string
 sh-4.2 $ echo "*txt"
*txt 

However, " i.e. double quotes, $ i.e. dollar, ` backtick and \ backslash are still interpreted by the shell, even when they are in double quotes.

Thus for displaying the above characters we need a backslash (\) as a special character. Thus, they wont be interpreted by the shell, but passed as a string and not as a command or wild character.

Let us see if we want an output as below:

A sentence with ", backslash \, backtick ` and few spaces are      alongwith dollar sign $

we would have to write a echo command as:

echo "
A sentence with \", backslash \\, backtick \` and few spaces are      alongwith dollar sign \$"

Thus, backslash itself must be escaped to show that it is to be taken literally.

Regards,
Niraj Bhagchandani
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Email ThisBlogThis!Share to XShare to Facebook
Newer Post Older Post Home

0 comments:

Post a Comment

Amazon Ads

Popular Posts

  • 12 differnet examples of For Loop in Shell Script
    The Link to programs given in this tutorial is:  https://goo.gl/akbcTw Well, to begin with FOR LOOP, I would tell that there are 2 diff...
  • Powerline – Adds Powerful Statuslines and Prompts to Vim Editor and Bash Terminal
    Powerline is a great statusline plugin for Vim editor , which is developed in Python and provides statuslines and prompts for many other...
  • How to install Docker and run Docker containers on Linux Mint 18/18.1
         How to install Docker and run Docker containers on Linux Mint 18/18.1 So, now before going through all the steps update and upg...
  • While Loop in Shell Script
    To run the given example live online please visit the following link: https://goo.gl/mD69Gj There are basic loops in shell script. For ...
  • Until Loop in Shell Script.
    To run these programs online I request you to go to following link: https://goo.gl/xKheU3 The until loop is similar to while loop in shel...
  • Variables in Shell Script - Part 1
      Every Programming language in existence has the concepts of variable – It can be termed as a chunk of memory which can be assigned valu...
  • Escape character in Shell Script
    Okay!! uptil now we have seen a lot of stuffs regarding Shell Script. But how about escape characters Example: What if you want to displa...
  • Wildcards
     A wildcard is a character that can be used as a substitute for any of a class of characters in a search, thereby greatly increasin...
  • My First Shell Script.
    My First Shell script We are about to write a script for Hello World. After that we will have some flavors of Hello World Program. Fir...

Recent Posts

Categories

  • Docker in Mint 18.1
  • Docker Linux Mint
  • Install Docker
  • Install Docker in Mint 18
  • Install Docker Linux Mint
  • tecmint

Pages

  • Home

Text Widget

Blog Archive

  • ►  2017 (1)
    • ►  June (1)
  • ▼  2016 (8)
    • ►  November (1)
    • ▼  October (7)
      • Until Loop in Shell Script.
      • While Loop in Shell Script
      • 12 differnet examples of For Loop in Shell Script
      • Escape character in Shell Script
      • Wildcards
      • Variables in Shell Script - Part 1
      • My First Shell Script.

Page Views:

About Me

Niraj Bhagchandani
View my complete profile

Search Here:

Powered by Blogger.

Sample Text

Copyright © Everything Shell Script | Powered by Blogger
Design by Hardeep Asrani | Blogger Theme by NewBloggerThemes.com | Distributed By Gooyaabi Templates