Link to home
Start Free TrialLog in
Avatar of ittechlab
ittechlabFlag for Canada

asked on

single quote and double quotes

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.
SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Avatar of Dave Gould
Dave Gould

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
Avatar of ittechlab

ASKER

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?
How many spaces before the 8192 ?
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"
i did count the space and tried. two spaces between BLCKSZ and 8192.
not working.

[root@rhel5 include]# egrep "#define BLCKSZ {1,}8192" pg_config_manual.h
[root@rhel5 include]#
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
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"
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
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.
sed 's/#define BLCKSZ[[:space:]]\{1,\}8192/#define BLCKSZ 32768/' pg_config_manual.h
I tried did't work.
Which one of the above suggestions do you mean?
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
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.
>> 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).
[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
Sorry, must be "sed -ri ..."
[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.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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 ...