boramalper boramalper - 21 days ago 8
Bash Question

Splitting a String into Command Line Arguments

I am trying to split a sting from the command-line history of the user into command line arguments. For instance, I want to split

program argument escaped\ argument.txt -o 'another escaped.output'


into

$v[1]: program
$v[2]: argument
$v[3]: escaped argument.txt
$v[4]: -o
$v[5]: another escaped.output


I have tried every single possible solution that I could, but as fish quotes the variables automatically, none of my solutions worked.

Thanks!

Answer

This is where you need eval.

$ set s "program argument escaped\ argument.txt -o 'another escaped.output'"
$ eval set v $s
$ printf "%s\n" $v
program
argument
escaped argument.txt
-o
another escaped.output