3

I'm writing a script to run a given program on a simulator while changing the argument supplied and I'm having a problem where bash keeps inserting single ticks around the double quotes that causes the simulator to crash spectacularly. How can I stop this from happening?

Script

#!/bin/bash
#./matrix_sim MM1.x86 1024 X86 gem5.opt

PROGRAM=$1
ARGUMENT=$2
ARCHI=$3
TARGET=$4

./gem5/build/$ARCHI/$TARGET gem5/configs/example/se.py -c ./$PROGRAM -o \"$ARGUMENT\"

When I run this script, what actually is output is

./gem5/build/X86/gem5.opt gem5/configs/example/se.py -c ./MM1.x86 -o '"2"'

But what I really want to happen is this

./gem5/build/X86/gem5.opt gem5/configs/example/se.py -c ./MM1.x86 -o "2"

How do I get rid of the single ticks on output?

Nubcake
  • 143
  • Do you want the program to actually get "2" as the option, or is 2 the right thing to pass? If the latter, just use "$ARGUMENT". – Kusalananda Feb 06 '17 at 17:44
  • Does the script need to hang around? If not, use exec ./gem5/... to make it replace itself with that final command. – thrig Feb 06 '17 at 17:59
  • You mean, in the debug output of bash -x? – Stéphane Chazelas Sep 22 '17 at 17:32
  • While the question is unclear, somehow google still finds it, someone who understands this should edit it, and not just bash -x, but also set -x in the script causes this behavior – tjb Oct 11 '18 at 09:13

3 Answers3

3

Bash does not add single quotes to your string. The single quotes are how it makes the output of the set -x trace visually unambiguous. In bash, set -x produces visually unambiguous output by putting single quotes around strings that contain shell special characters (whitespace and !"#$&\()*;<>?[\]^`{|}~). Here, you're tracing a command which takes a parameter containing a ", so bash prints it out with single quotes around it to make it clear that the " is a literal character in the string and not something in the shell source syntax.

It seems that you didn't want to have a double quote in the argument. So don't put one.

You do need double quotes around variable expansions, but that's double quotes, in the shell syntax, not putting a quoted double quote so that a double quote character ends up in the string.

"./gem5/build/$ARCHI/$TARGET" gem5/configs/example/se.py -c "./$PROGRAM" -o "$ARGUMENT"
1

Just use "$ARGUMENT":

./gem5/build/$ARCHI/$TARGET gem5/configs/example/se.py -c ./$PROGRAM -o "$ARGUMENT"

Bash will add single quotes (in e.g. the set -x tracing output) when you use \"$ARGUMENT\" because you're making the double quotes part of the argument's value (this is most likely not what you want).

Kusalananda
  • 333,661
0

Unable to replicate:

$ ./in.sh program arg arch target
./gem5/build/arch/target gem5/configs/example/se.py -c ./program -o "arg"
$ cat in.sh
#!/bin/bash
#./matrix_sim MM1.x86 1024 X86 gem5.opt

PROGRAM=$1
ARGUMENT=$2
ARCHI=$3
TARGET=$4

echo ./gem5/build/$ARCHI/$TARGET gem5/configs/example/se.py -c ./$PROGRAM -o \"$ARGUMENT\"

I've tried many manipulations to try to replicate what you're seeing and am unable to do so.

You could perhaps try:

ARGUMENT="\"$2\""

and then removing the escaped quotes around your invocation:

echo ./gem5/build/$ARCHI/$TARGET gem5/configs/example/se.py -c ./$PROGRAM -o "$ARGUMENT"
DopeGhoti
  • 76,081