Bash- Command run from script does not pass full parameters with spaces inside

There's a JavaScript file that I call from command line (there's a framework) like so:

./RunDiag.js param1:'string one here' param2:'string two here'

I have a shell script where I invoke the above command. I can run it in a script as simple as this

#!/bin/bash

stuff="./RunDiag.js param1:'string one here' param2:'string two here'"
$stuff

When I run this within a script, the parameters are all truncated, each becoming 'string, instead of the entire parameter. Why? When I run this command from command line, no such thing occurs.

---------- Post updated at 03:31 PM ---------- Previous update was at 02:03 PM ----------

Having done more testing, it seems like that every time there's a space between items in the string, such as "string one", the spaces are converted into commas: string,one - any idea what might be causing this?

The shell is field splitting the unquoted variable reference, using $IFS. Try:

eval "$stuff"
1 Like

Why so complicated?

Try:

#!/bin/bash
./RunDiag.js 'param1:string one here' 'param2:string two here'

That script is really just a reduced and obfuscated equivalent to what my full script is doing- it's building each part of that command piece by piece, including the parameter list, which is parsed elsewhere. When I run it in my shell script like

$RunDiag ${params[@]}

it fails because if any of the parameters are strings that are like 'string one', the spaces are overwritten sometime in the process into commas.

I've found a solution: change the default IFS temporarily when I run that command, and it works.

Edit: You're right, scrutinizer! It took me some time looking it up but that was the solution, yeah.

Good, but since you are using arrays, did you try:

$RunDiag "${params[@]}"