Bash shell adding extra single quotes


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash shell adding extra single quotes
# 1  
Old 07-12-2012
Bash shell adding extra single quotes

AIX 6.1
bash shell

Code:
#!/bin/bash -x
STATEMENT="cvs commit -m \"This is\" ../PBP/EIR.ENTRY"
echo $STATEMENT
exit 0

This is the output
+ STATEMENT='cvs commit -m "This is" ../PBP/EIR.ENTRY'
+ echo cvs commit -m '"This' 'is"' ../PBP/EIR.ENTRY
cvs commit -m "This is" ../PBP/EIR.ENTRY
+ exit 0

Problem: Notice the single quotes between "This" and "is" ... This causes the called program "cvs" to think those are separate parameters which they are not.

If I change the shell to this
Code:
#!/bin/bash -x
STATEMENT="cvs commit -m \"This is\" ../PBP/EIR.ENTRY"
echo $STATEMENT
$STATEMENT
exit 0

Then this is the ouput

+ STATEMENT='cvs commit -m "This is" ../PBP/EIR.ENTRY'
+ echo cvs commit -m '"This' 'is"' ../PBP/EIR.ENTRY
cvs commit -m "This is" ../PBP/EIR.ENTRY
+ cvs commit -m '"This' 'is"' ../PBP/EIR.ENTRY
cvs commit: nothing known about `is"'
cvs [commit aborted]: correct above errors first!
+ exit 0

Please do not pass this off as a "cvs" problem. It is not. If I change the script to this
Code:
#!/bin/bash -x
STATEMENT="cvs commit -m \"Thisis\" ../PBP/EIR.ENTRY"
echo $STATEMENT
$STATEMENT
exit 0

Then this is the output
+ STATEMENT='cvs commit -m "Thisis" ../PBP/EIR.ENTRY'
+ echo cvs commit -m '"Thisis"' ../PBP/EIR.ENTRY
cvs commit -m "Thisis" ../PBP/EIR.ENTRY
+ cvs commit -m '"Thisis"' ../PBP/EIR.ENTRY
+ exit 0

How do I get bash to ignore that space?

Last edited by joeyg; 07-12-2012 at 03:31 PM.. Reason: Please wrap data and sripts with CodeTags
# 2  
Old 07-12-2012
Did you try to enclose your $STATEMENT into double quote ?
Code:
echo "$STATEMENT"

What are you trying to achieve ?

If you remove the -x option of your bash command, doesn't your script behave the way you expect ?
# 3  
Old 07-12-2012
-x does not make a difference. I have done both -x and +x and no'x' with the same results.

What I need to do is actually execute $STATEMENT
# 4  
Old 07-12-2012
bash does not parse quotes inside quotes or other forms of that sort of doublethink. To make it do so, you use eval.

The reason it doesn't parse quotes inside quotes and the like is the same reason eval tends to be avoided -- parsing syntax inside syntax leaves you open to someone injecting a `rm -Rf ~/` into your input and having your code execute that.
# 5  
Old 07-12-2012
I might try something like this:

Code:
set -- cvs commit -m "This is" ../PBP/EIR.ENTRY

"$@"

...to preserve tokens (not quotes -- the whole point is you don't want quotes) as demanded.
# 6  
Old 07-12-2012
NOTE:
cvs = Concurrent Versions System
commit = commit to the cvs repository
-m = What follows in the quotes is the Log message for this revision
"This is" = the Log message for this revision
./q.sh = the file

This works:
Code:
#!/bin/bash -x
set -- cvs commit -m "This is the comment" ./q.sh 
"$@"
exit 0

What I want to be able to do is dynamically create that whole string in the script and execute it.

This fails
Code:
#!/bin/bash -x
STATEMENT="set -- cvs commit -m \"This is the comment\" ./q.sh "
"$STATEMENT"
exit 0



This fails
Code:
#!/bin/bash -x
STATEMENT="set -- cvs commit -m \"This is the comment\" ./q.sh "
$STATEMENT
exit 0

This is so frustrating because it seems so simple. I am feeling like a not so bright person right now. Smilie

Last edited by Scrutinizer; 07-12-2012 at 05:43 PM.. Reason: code tags
# 7  
Old 07-12-2012
(Referring to your last post) Why are you using set -- in the assignment to $STATEMENT?

Try:
Code:
STATEMENT="cvs commit -m \"This is the comment\" ./q.sh "
eval "$STATEMENT"

or, in the spirit of the examples:
Code:
STATEMENT="set -- cvs commit -m \"This is the comment\" ./q.sh "
eval "$STATEMENT"
"$@"


Last edited by Scrutinizer; 07-12-2012 at 06:20 PM.. Reason: Added eval statement to correctly parse the quotes, as Corona correctly pointed out...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Bash not recognizing single quotes in Mac?

Hi, I just bought a new mac and have been running a program out of terminal, but even early on I noticed that my single quotes looked a lot different from the ones used in all of the namelists and other files of the program. Specifically, mine are kind of slanted whereas the others are very... (7 Replies)
Discussion started by: jtcastro99
7 Replies

2. Shell Programming and Scripting

Issue with Single Quotes and Double Quotes for prompt PS1

Hi, Trying to change the prompt. I have the following code. export PS1=' <${USER}@`hostname -s`>$ ' The hostname is not displayed <abc@`hostname -s`>$ uname -a AIX xyz 1 6 00F736154C00 <adcwl4h@`hostname -s`>$ If I use double quotes, then the hostname is printed properly but... (3 Replies)
Discussion started by: bobbygsk
3 Replies

3. Shell Programming and Scripting

shell script - to append single quotes and comma

file1 ---- 34556745 32678343 31576776 31455566 21356666 I want to assign the record values to a variable in the below format, so that I can use output in .sql file for querying in database. ('34556745', '32678343', '31576776', '31455566', '21356666') ----------- below is the... (11 Replies)
Discussion started by: rajivrsk
11 Replies

4. Shell Programming and Scripting

Having a terrible problem with quotes/single quotes!

Hello. I'm trying to write a bash script that uses GNU screen and have hit a brick wall that has cost me many hours... (I'm sure it has something to do with quoting/globbing, which is why I post it here) I can make a script that does the following just fine: test.sh: #!/bin/bash # make... (2 Replies)
Discussion started by: jondecker76
2 Replies

5. Shell Programming and Scripting

Replace single quote with two single quotes in perl

Hi I want to replace single quote with two single quotes in a perl string. If the string is <It's Simpson's book> It should become <It''s Simpson''s book> (3 Replies)
Discussion started by: DushyantG
3 Replies

6. UNIX for Dummies Questions & Answers

grep single quotes or double quotes

Unix superusers, I am new to unix but would like to learn more about grep. I am very familiar with regular expressions as i have used them for searching text files in windows based text editors. Since I am not very familiar with Unix, I dont understand when one should use GREP with the... (2 Replies)
Discussion started by: george_vandelet
2 Replies

7. Shell Programming and Scripting

removing extra double quotes between pipe dilimeter

I have a flat file sample like this - "COURSE"|"ddddd " " dddd"|"sssddd sdsdsdsdx" dddddddd ffffff "aaaaa" dddddddd ffffff sdsdsd"|"xxxxxxx"| "COURSE"|"ffff " " bbbb"|"lllll"| The delimiter is pipe character (|) and the text are enclosed in double quotes... (5 Replies)
Discussion started by: vishalzone
5 Replies

8. Shell Programming and Scripting

Single quotes and double quotes

Hi guys, I have a sed line in double quotes which works fine, but I want it to be in single quotes here is the sed line sed "/abc_def/s/\'.*\'/\'\${abc_def}\'/" can some one give the equivalent to the above script in single quotes Thanks a ton (5 Replies)
Discussion started by: sol_nov
5 Replies

9. Shell Programming and Scripting

How can i use single quotes for SQL command in shell script

Hi. please help me to write the following query in a shell script. the Query is :select no,salary from emp_info where name='$var_name' the following is my code. #! /bin/sh var_name=$1 sqlplus -s user/pwd@DB << EOF select no,salary from emp_info where name="'$var_name'";... (4 Replies)
Discussion started by: little_wonder
4 Replies

10. Shell Programming and Scripting

Double quotes or single quotes when using ssh?

I'm not very familiar with the ssh command. When I tried to set a variable and then echo its value on a remote machine via ssh, I found a problem. For example, $ ITSME=itsme $ ssh xxx.xxxx.xxx.xxx "ITSME=itsyou; echo $ITSME" itsme $ ssh xxx.xxxx.xxx.xxx 'ITSME=itsyou; echo $ITSME' itsyou $... (3 Replies)
Discussion started by: password636
3 Replies
Login or Register to Ask a Question