-   Programming (
-   -   escaping a single quote from a bash script (

atbrew 07-21-2010 07:35 AM

escaping a single quote from a bash script
There is probably a very simple fix, but its wrecking my buzz

I call the following on the command line

R CMD BATCH '--args Y_filename="one.txt" out="two.txt"' brew.R

I want to call this from a bash script but replace one.txt with $1, and two.txt with $2 (ie the first two arguments from the command line)

I have tried in vane

"R CMD BATCH '--args Y_filename=\"$1\" out=\"$2\"' brew.R"

any ideas?

Thanks in advance,

miedward 07-21-2010 07:40 AM


Originally Posted by atbrew (Post 4040649)

"R CMD BATCH '--args Y_filename=\"$1\" out=\"$2\"' brew.R"

`R CMD BATCH \'--args Y_filename=\"$1\" out=\"$2\"\' brew.R`

pixellany 07-21-2010 07:57 AM

Backtics ( ` ) are normally used for command substition, but "$(......)" is now preferred.

Regardless, I'm not sure how this will address the question.

What is the command being run? eg is it "R" or "R CMD BATCH"? If the latter, then what stops the shell from just trying to run "R"?

second, why the single quotes in your first example?

atbrew 07-21-2010 08:05 AM

miedward sorry that aint working...

pixellany so I am calling a stats program call R

the arguments CMD and BATCH are arguments for R, the bit in single quotes are the arguments R then passes on to the R script brew.R

so as I said from the command line I call

R CMD BATCH '--args Y_filename="one.txt" out="two.txt"' brew.R

and I just want/need to be able to replicate that from within a bash script.

atbrew 07-21-2010 08:11 AM

Horrible hack
So the horrible hack we have that works is


CMD="/usr/bin/R CMD BATCH '--args input=\"${1}\" output=\"${2}\"' brew.R"
echo $CMD >
exec sh

pixellany 07-21-2010 08:14 AM

Again, why the single quotes? (And why is --args inside the quotes?)

Will any of these work?


R CMD BATCH --args Y_filename=one.txt out=two.txt brew.R

R CMD BATCH --args "Y_filename=one.txt out=two.txt" brew.R

R CMD BATCH --args "Y_filename=$1 out=$2" brew.R

grail 07-21-2010 08:19 AM

So I am a little bit confused (this is easily done some times), but why not just alternate your quotes to get what you want?

/usr/bin/R CMD BATCH '--args input='"${1}"' output='"${2}" brew.R
This type works when using say awk and trying to include script variables. You may need to play with this a bit as I do not have the R program
so cannot test.

atbrew 07-21-2010 08:28 AM

grail that worked, thanks a million,

grail 07-21-2010 09:21 AM

No probs ... please remember to mark as SOLVED once you have a solution.

All times are GMT -5. The time now is 11:35 AM.