1

I am writing a shell script to help me running with the react-native project, my script has the following parts:

#some steps to create $calculated_value...

os_type=$1
shift
script="$calculated_value react-native run-$os_type $@"
$script

When I type this command in terminal:

./scripts/run ios --simulator="iPhone 6"

I expect the script should execute the following command at the end:

"$calculated_value" react-native run-ios --simulator="iPhone 6"

However, the scripts now execute this command:

"$calculated_value" react-native run-ios --simulator=iPhone 6

which the double quotes have disappeared, and terminal just read the simulator without the "6".

I have tried replacing the $@ with $(for i;do echo ${i@Q};done;)
but it gives me the error: line 25: ${i@Q}: bad substitution

I know I can add \ before the double quotes to escape it
but just want to see if there are any solutions I can do to skip adding \.

---------------------------------------------- EDIT INFO ----------------------------------------------

Now I am using eval inside the script in order to run the command with a file location.
Inside the scripts, it contains the below steps:

...some calculation to get $envfile

os_type=$1
shift

run_script="ENVFILE=$envfile react-native run-$os_type $@"
eval $run_script

Inside my package.json:

{
  "scripts": {
    "dev": "./scripts/dev"
  }
}

In my real case, when I type:

npm run dev ios -- --simulator="iPhone 6"  

What I expected is:

ENVFILE=env/tw.dev react-native run-ios --simulator="iPhone 6"

But now the command miss the double dash argument.

Wing Choy
  • 900
  • 10
  • 25
  • 1
    Escaping won't help you. Use an array instead – choroba Sep 26 '19 at 07:11
  • Sorry but I don't get your meaning, could you write an example for me? Thanks. – Wing Choy Sep 26 '19 at 07:14
  • 2
    Don’t put commands in variables. Variables are for data, not commands. See [bashFAQ#50: I'm trying to put a command in a variable, but the complex cases always fail!](https://mywiki.wooledge.org/BashFAQ/050) – Gordon Davisson Sep 26 '19 at 07:20
  • @tripleee In my case, the double dash is inside the argument. Therefore the question you quoted for me is not working. – Wing Choy Oct 10 '19 at 03:20
  • Why do you think the location of the dashes is important? This is about quoting. But I'll add another popular duplicate; this is a relatively common question. – tripleee Oct 10 '19 at 03:52

1 Answers1

0

Consider eliminating the 'script' intermediate variable

os_type=$1
shift
"$calculated_value" react-native "run-$os_type" "$@"

The quotes around 'run-$os_type', and '$@' will address proper expansion of parameters with spaces (or other 'magic' characters: *, ?, ...).

It's not clear what is the value of "$calculated_value", answer assume it's the path to a program (without extra arguments). If it may contain arguments, or options, you will probably have to remote the quotes around it.

EDIT 2019-10-10:

With the original code ("script=... $@"), bash will break the input arguments (the '$@') into tokens in the assignment command. Possible alternative to prevent this step is:

script="$calculated_value" react-native "run-$os_type"
$script "$@"
dash-o
  • 13,723
  • 1
  • 10
  • 37
  • Sorry, I am using eval before the script, as there is a file location on the calculated value. Is there a way to use your solution in eval? Thanks. – Wing Choy Oct 09 '19 at 08:04
  • Can you post the full eval command, it is not referenced in the question – dash-o Oct 09 '19 at 16:54
  • Hello, I just update my real case on the question. Thank you. – Wing Choy Oct 10 '19 at 02:56
  • @WingChoy note proposed solution was to replace 'script=some-command ; $script' with 'eval some-command'. Using 'eval' on '$script' may not help, quotes will be stripped in this sequence – dash-o Oct 10 '19 at 04:49