Went through this post: Pass arguments to function exactly as-is
But have a slightly different setup:
I have 3 bash functions foo, bar, baz. They are setup as follows:
foo() {
bar $1
}
bar() {
var1=$1
var2=$2
echo "$var1" test "$var2"
}
export ENV_VAR_1="1"
export ENV_VAR_2="2 3"
foo "${ENV_VAR_1} ${ENV_VAR_2}"
I'd expect the output to be:
1 test 2 3
But the output is:
1 test 2
I get why this happened. bar was executed as follows:
bar 1 2 3
My question is: how do I get it to execute
bar 1 "2 3"
Approaches I tried:
foo () {bar "$1"}
# Out: 1 2 3 test. Makes sense since "1 2 3" is interpreted as a single argument.
foo () {bar "$1"}
is a syntax error. If you want a one-line function definition, you needfoo() { bar "$1";}
Note the space and the semicolon. – Wildcard Sep 07 '16 at 01:07