Solved

single quote and double quotes

Posted on 2014-02-14
23
210 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

This Windows batch file is useful for organizing image files from a digital camera or other source, but can have many other uses.  It simply renames the file(s) to match their create date.  For example, if you took a picture today at 1:40pm and the …
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…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

757 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

19 Experts available now in Live!

Get 1:1 Help Now