OS: Ubuntu 16.04.3
Shell: Bash 4.3.48
I know that is possible to temporarily change the content of a variable as in var=value command
, being probably IFS= read -r var
the most notable case of this.
And, thanks to Greg's wiki, I also understand:
# Why this
foo() { echo "$var"; }
var=value foo
# And this does work
var=value; echo "$var"
# But this doesn't
var=value echo "$var"
What escapes my understanding is this:
$ foo() { echo "${var[0]}"; }
$ var=(bar baz) foo
(bar baz)
As far as I know (and following the logic of previous examples), it should print bar
, not (bar baz)
.
Does this only happen to me? Is this the intended behavior and I'm missing something? Or is this a bug?
export var=(foo bar); echo "${var[0]}"
it printsfoo
, not(foo bar)
.export
it shows:declare -ax var=([0]="foo" [1]="bar")
export i_am_array=(foo bar); /usr/bin/env | grep i_am_array
gives no output here.foo() { declare -p var; } ; var=(bar baz) foo
givesdeclare -x var="(bar baz)"
confirming its being treated as a string, not an array