Solved

single quote and double quotes

Posted on 2014-02-14
23
215 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Powershell script to return group membership 8 70
Continuous Ping 10 130
Red Hat script request 23 76
Find non-UTF Filenames 1 50
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 …
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
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…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

920 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now