ittechlab
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.
I am trying to understand when to use single and when to use double. give me some examples and scenario please.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
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/p g_config_m anual.h
#define BLCKSZ 8192
[root@rhel5 scripts]# grep '#define BLCKSZ 8192' /usr/local/src/postgresql- 8.2.10/src /include/p g_config_m anual.h
[root@rhel5 scripts]# ---->>> not getting any output. why?
[root@rhel5 scripts]# grep '#define BLCKSZ' /usr/local/src/postgresql-
#define BLCKSZ 8192
[root@rhel5 scripts]# grep '#define BLCKSZ 8192' /usr/local/src/postgresql-
[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"
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"
ASKER
i did count the space and tried. two spaces between BLCKSZ and 8192.
ASKER
not working.
[root@rhel5 include]# egrep "#define BLCKSZ {1,}8192" pg_config_manual.h
[root@rhel5 include]#
[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/p g_config_m anual.h
Or, if there are other invisible characters we are not seeing
grep '#define BLCKSZ' /usr/local/src/postgresql- 8.2.10/src /include/p g_config_m anual.h | od -c
Or, if there are other invisible characters we are not seeing
grep '#define BLCKSZ' /usr/local/src/postgresql-
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"
grep -P "#define BLCKSZ[ \t]{1,}8192" pg_config_manual.h
"grep -P" means "interpret PATTERN as a Perl regular expression"
ASKER
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
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.
"-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,\}819 2/#define BLCKSZ 32768/' pg_config_manual.h
ASKER
I tried did't work.
Which one of the above suggestions do you mean?
ASKER
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
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.
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).
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).
ASKER
[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
[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 ..."
ASKER
[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.
[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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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 ...
I assume there is, and if so it should contain the original data - in case you want to start over ...
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