Bash array export? -


is possible declare , export array single statement within function?

my current workaround first declare, export.

f() { foo=(1 2 3); export foo; }; f; export -p | grep foo= declare -ax foo='([0]="1" [1]="2" [2]="3")' 

i observe that:

f() { export bar=(1 2 3); }; f; export -p | grep bar= <no output> 

and:

f() { export baz="(1 2 3)"; }; f; export -p | grep baz= declare -x baz="(1 2 3)" # not array 

i use v3.2.48(1)-release , can't upgrade.


some background:

i have friend whome trying study "some" django.

he's more clueless me @ command line , needs following, on osx hackintosh:

  • launch interactive shell
  • find path variable including django bin dir, per specification
  • find updated pythonpath env var, various django libs visible
  • a nice interactive ipython shell start typing commands in after double-clicking
  • (tricky) interactive shell fall once ctrl-d exits ipython

on windows, alias command shortcut, cmd.exe, set custom environment variables , start ipython. works: after exiting ipython 1 still finds oneself in command interpreter.

is possible @ osx's standard bash? played bash -c many things don't work, changing directory, being able exit python , stay in terminal, etc. played -s.

you can't export array in bash (or other shell). bash never export array environment (until maybe implemented someday, see bugs section in manpage).

there couple things going on here. again, setting -x attribute on array nonsensical because won't exported. only reason you're seeing results because defined array before localizing it, causing drop down next-outermost scope in name has been made local (or global scope).

so clear, whenever use declaration command, you're creating local except when use export or readonly non-array assignment argument, because these posix, doesn't specify either locals or arrays.

function f {     typeset -a # "a" local "f"     g     printf 'now in "f": %s\n' "$(typeset -p a)" }  function g {     a=(1 2 3)                               # assigning f's localized "a"     typeset -a                            # new local     printf 'in "g": %s\n' "$(typeset -p a)" # g's local empty.     a=(a b c)                               # set g's local new value.     printf 'still in "g": %s\n' "$(typeset -p a)" }  f  # in "g": declare -a a='()' # still in "g": declare -a a='([0]="a" [1]="b" [2]="c")' # in "f": declare -a a='([0]="1" [1]="2" [2]="3")' 

it seems if give array argument export bash make local other declaration command. don't use export define array. export posix builtin , behavior undefined arrays, why bash treats though had used typeset -ax a=(1 2 3). use typeset, local, or declare. there's no way know what's "correct", or compare other shells. ksh93 other accepts array assignments arguments declaration commands, , behavior doesn't agree bash.

the important thing understand environment has nothing it, you're playing quirks of locals when trying nonstandard things posix-only commands.

in order effect want, may use typeset -p.

function f {     typeset -a "${1}=(1 2 3)"     typeset -p "$1" }  typeset -a arr eval "$(f arr)" typeset -p arr 

bash guarantees you'll out correct result, find isn't useful , use approach. it's better let caller define local , use dynamic scope work (as you've discovered... without exporting).

in bash 4.3 can use typeset -n, correct way handle this.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -