Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

read all shell variables in ruby

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.

like image 983
mpapis Avatar asked Nov 24 '25 10:11

mpapis


2 Answers

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 bash and zsh. So there is no single way to do this across all POSIX shells, and we need to check $BASH_VERSION etc. as you already noted.

I decided that it was better to avoid having to write Ruby to parse the output of set or other shell built-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 master branch 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 yaml branch and developed my own implementation. Again I extended the tests. They all pass ;-) Notice that I use a few different tricks to do introspection in zsh and bash:

  • zsh has a zsh/parameter module which provides associative arrays containing the names and types of all its parameters.
  • bash has declare -p which is in an easily parseable form. It also has compgen -A variable, but in the end I didn't use this.

I think it would be easy to add ksh support too.

like image 189
Adam Spiers Avatar answered Nov 25 '25 23:11

Adam Spiers


set returns all shell variables, not environment variables. To get environment variables, use the env command. Note that bash does not export arrays to the environment.

like image 39
chepner Avatar answered Nov 25 '25 23:11

chepner