Wednesday, January 29, 2014

Unix Shell Scripting - Trouble with Quotes



Trouble with Quotes in Unix shell Scripting:

- In Unix shell scripting, there are 3 types of quotes
    - single quote -  '
    - Double quote -  "
    - Back quote   - `

- Single Quotes - The text enclosed in single quote is considered to be normal text including the special characters like - $, #, *, [, space character(space is a special character used to identify the list of variables), newline character except for another single quote(')
[oracle@rac1 ~]$ echo 'The user logged in is $LOGNAME'
The user logged in is $LOGNAME

- Double Quotes - The double quote will maintain the special characters as is, like $, `
[oracle@rac1 ~]$ echo "The user logged in is $LOGNAME"
The user logged in is oracle

To remove the special meaning of characters used in double quotes those need to be escaped using backslash \
[oracle@rac1 ~]$ echo "The user logged in is \$LOGNAME"
The user logged in is $LOGNAME


- Back Quotes - 
- The commands mentioned in the back quotes are treated as Unix commands and executed in a new shell and output is displayed on to the shell from which the command is run

[oracle@rac1 ~]$ echo "Todays date is `date`"
Todays date is Thu Jan 30 08:03:16 IST 2014

- The output generated by the commands in back quotes will not have the newlines, multiple spaces or tabs are replaced with single space.

[oracle@rac1 ~]$ who | sort
oracle   pts/0        2014-01-27 07:47 (:0.0)
oracle   tty1         2014-01-27 07:45 (:0)

[oracle@rac1 ~]$ list=`who | sort`
[oracle@rac1 ~]$ echo $list
oracle pts/0 2014-01-27 07:47 (:0.0) oracle tty1 2014-01-27 07:45 (:0)

No comments:

Post a Comment