Zhani Baramidze Zhani Baramidze - 22 days ago 8
Bash Question

space as parameter to program from bash

I'm running application from bash, like this

./app_name var1 var2 $var3


where
var3
is
--a b, --c d
and where
b
and
d
don't contain spaces.

Now I want to add parameter,
--e f
, to
$var3
, but
f
contains whitespace. The problem is that I'm trying to avoid changes in this line.

./app_name var1 var2 $var3


If I leave it like this,
app_name
understands two parts of
f
as two different arguments, and thus it fails to parse.

Can I change
var3
somehow so that it doesn't split
f
as two different args? I have tried putting quotes inside, escaping space but it didn't help.

Answer

You can achieve that by making var3 an array, for example:

var3=(--a b --c d --e "f1 f2")

./app_name var1 var2 "${var3[@]}"

This way f1 f2 will be treated as a single argument.

Comments