Problem:
I’m running shell script as subprocess in ruby script, after running script I want to have an option to check all environment variables of the shell, including array variables.
So far I have come up with:
set | awk -F= 'BEGIN {v=0;}
/^[a-zA-Z_][a-zA-Z0-9_]*=/ {v=1;}
v==1 && $2~/^['\''\$]/ {v=2;}
v==1 && $2~/^\(/ {v=3;}
v==2 && /'\''$/ && !/'\'\''$/ {v=1;}
v==3 && /\)$/ {v=1;}
v {print;}
v==1 {v=0;}
'
Which quite good shows only variables, including arrays, multiline strings and filtering out functions.
But this does not use the same format all the time, especially array variables are represented differently in BASH and ZSH.
Here is my current implementation: https://github.com/mpapis/tf/blob/master/lib/tf/environment.rb
Question:
Is there an easy way to show all the variables that will work persistently in BASH and ZSH / possibly other shells.
Nice to see you again mpapis 😉
Unfortunately arrays and associative arrays are not covered by POSIX.1-2008, and as you have found there are some annoying subtle differences between
bashandzsh. So there is no single way to do this across all POSIX shells, and we need to check$BASH_VERSIONetc. as you already noted.I decided that it was better to avoid having to write Ruby to parse the output of
setor othershellbuilt-ins. The output is not convenient to parse, and anyway the shell knows the most about its own data, so I thought it made sense to put most of the intelligence inside the shell code. So instead I have come up with a solution which uses shell code to output the data structures as YAML, and then that YAML gets loaded directly into Ruby.First I imported your reference implementation and tests into the
masterbranch of a standalone repository. Then I beefed up the test suite and made a few tweaks. This showed that there are still issues with the multi-line handling.Then I created a new
yamlbranch and developed my own implementation. Again I extended the tests. They all pass 😉 Notice that I use a few different tricks to do introspection inzshandbash:zshhas azsh/parametermodule which provides associative arrays containing the names and types of all its parameters.bashhasdeclare -pwhich is in an easily parseable form. It also hascompgen -A variable, but in the end I didn’t use this.I think it would be easy to add
kshsupport too.