Solved

single quote and double quotes

Posted on 2014-02-14
23
219 Views
Last Modified: 2014-02-28
what is main difference of usage of single quote and double quote in shell script.

I am trying to understand when to use single and when to use double. give me some examples and scenario please.
0
Comment
Question by:ittechlab
  • 10
  • 8
  • 3
  • +1
23 Comments
 
LVL 84

Assisted Solution

by:ozo
ozo earned 100 total points
ID: 39859235
in bash
       Enclosing characters in single quotes preserves the literal value of each character within the quotes.  A single quote may not occur between single quotes, even when preceded by a backslash.

       Enclosing characters in double quotes preserves the literal value of all characters within the quotes, with the exception of $, `, \, and, when history expansion is enabled, !.  The characters  $  and  `
       retain  their  special  meaning  within  double  quotes.  The backslash retains its special meaning only when followed by one of the following characters: $, `, ", \, or <newline>.  A double quote may be
       quoted within double quotes by preceding it with a backslash.  If enabled, history expansion will be performed unless an !  appearing in double quotes is escaped using a backslash.  The backslash preceding the !  is not removed.
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 39859236
Use double quotes to keep the shell from filename globbing when the string/variable contains an asterisk (*, wildcard) or a question mark (?, single character) but embedded variables ($xxx) should be expanded nevertheless.

Single quotes keep the shell from interpreting metacharacters of any kind, also including double quotes.

Double as well as single quotes around a string containing spaces keep the shell from splitting up the string into single words.

Try

echo *

echo "*"

echo "* $USER"

echo '* $USER'

A=a b c
echo $A

A="a b c"
echo $A
0
 
LVL 5

Expert Comment

by:Dave Gould
ID: 39859246
Quotes are basically to group words together. If you use double quotes, then any variables within the quotes will still ne interpreted.
ie echo "this machine is $hostname"
will result in displaying the following text:
this machine is servera

but with single quotes, the variable is not interpreted
ie echo 'this machine is $hostname'
will result in displaying
this machine is $hostname

You also have the backtick ` character. this will interpret a command
ie echo "current user is `whoami` "
will give :
current user is mylogin
0
On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

 

Author Comment

by:ittechlab
ID: 39859254
why i am not getting the the whole word

[root@rhel5 scripts]# grep '#define BLCKSZ' /usr/local/src/postgresql-8.2.10/src/include/pg_config_manual.h
#define BLCKSZ  8192

[root@rhel5 scripts]# grep '#define BLCKSZ 8192' /usr/local/src/postgresql-8.2.10/src/include/pg_config_manual.h
[root@rhel5 scripts]#  ---->>> not getting any output. why?
0
 
LVL 5

Expert Comment

by:Dave Gould
ID: 39859262
How many spaces before the 8192 ?
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 39859266
Because there are two or more spaces between BLCKSz and 8192 and you're searching for just one space.

Try

 egrep "#define BLCKSZ {1,}8192" filename

{1,} means "match one or more occurrences of the preceeding character" (here: a space)

"egrep" (the same as "grep -E") means "interpret the pattern as an extended regular expression"
0
 

Author Comment

by:ittechlab
ID: 39859290
i did count the space and tried. two spaces between BLCKSZ and 8192.
0
 

Author Comment

by:ittechlab
ID: 39859295
not working.

[root@rhel5 include]# egrep "#define BLCKSZ {1,}8192" pg_config_manual.h
[root@rhel5 include]#
0
 
LVL 84

Expert Comment

by:ozo
ID: 39859305
grep '#define BLCKSZ  8192' /usr/local/src/postgresql-8.2.10/src/include/pg_config_manual.h
Or, if there are other invisible characters we are not seeing
grep '#define BLCKSZ' /usr/local/src/postgresql-8.2.10/src/include/pg_config_manual.h | od -c
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 39859336
Could be one or more <TAB>, or one or more <TAB> + one or more <SPACE>. or one or more <SPACE> + one or more <TAB>

grep -P "#define BLCKSZ[ \t]{1,}8192" pg_config_manual.h

"grep -P"  means "interpret PATTERN as a Perl regular expression"
0
 

Author Comment

by:ittechlab
ID: 39859366
thanks. that what is it.

if I am using sed to change the value should use this way? it was not working. please advise.

sed 's/#define BLCKSZ[ \t]{1,}8192/#define BLCKSZ[ \t]{1,}32768/' pg_config_manual.h
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 39859422
sed -r 's/#define BLCKSZ[ \t]{1,}8192/#define BLCKSZ\t32768/' pg_config_manual.h

"-r" tells "sed" to accept extended regular expressions.

Use "sed -ri ..." to change the file in place or redirect the output to a new file.
0
 
LVL 84

Expert Comment

by:ozo
ID: 39859426
sed 's/#define BLCKSZ[[:space:]]\{1,\}8192/#define BLCKSZ 32768/' pg_config_manual.h
0
 

Author Comment

by:ittechlab
ID: 39859429
I tried did't work.
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 39859443
Which one of the above suggestions do you mean?
0
 

Author Comment

by:ittechlab
ID: 39859452
when i run this command  why i am getting many outputs rather than the one changed. is that the behavior of the sed command?

sed -r 's/#define BLCKSZ[ \t]{1,}8192/#define BLCKSZ\t32768/' pg_config_manual.h
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 39859453
If "sed -r" is not supported:

sed  's/#define BLCKSZ\([ \t]\{1,\}\)8192/#define BLCKSZ \1 32768/' pg_config_manual.h

And please note that without "-i" the result is just written to stdout, the file remains unchanged.
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 39859464
>> when i run this command  why i am getting many outputs <<

You see the whole file with the relevant parts changed as desired.

To see just the changes use "sed -nr ..." (first suggestion) or "sed -n ..." (second suggestion).
0
 

Author Comment

by:ittechlab
ID: 39859465
[root@rhel5 include]# sed -ir 's/#define BLCKSZ[ \t]{1,}8192/#define BLCKSZ\t32768/' pg_config_manual.h
[root@rhel5 include]#
[root@rhel5 include]#
[root@rhel5 include]# grep "#define BLCKSZ" pg_config_manual.h
 

it didn't change
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 39859478
Sorry, must be "sed -ri ..."
0
 

Author Comment

by:ittechlab
ID: 39859505
[root@rhel5 include]# sed -i 's/#define BLCKSZ\([ \t]\{1,\}\)8192/#define BLCKSZ \1 32768/' pg_config_manual.h
[root@rhel5 include]# diff pg_config_manual.h /tmp/file
26c26
< #define BLCKSZ         32768
---
> #define BLCKSZ        8192

I notice the change now. is the new value with the tab or with spaces.
0
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 200 total points
ID: 39859518
It's preceeded by the same stuff as the original (but with spaces around, because I made a typo).

Should have been:

sed -i 's/#define BLCKSZ\([ \t]\{1,\}\)8192/#define BLCKSZ\132768/' ...
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 39859551
Check if there is a new file pg_config_manual.hr

I assume there is, and if so it should contain the original data - in case you want to start over ...
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question