fork download
  1. #!/bin/sh
  2. # If you only have a bash 4.x, you can test with compat 3.1 bash
  3. # shopt -s compat31
  4. FOO="var_with_two_words='foo bar' other_var='baz'"
  5. # An "env variable" definer that use the read command to parse and define the env variable
  6. define() {
  7. IFS=\= read -r key value <<EOF
  8. $1
  9. EOF
  10.  
  11. # Unquotting the value, adapt as it fit your needs
  12. value="${value#\'}"
  13. value="${value%\'}"
  14. read "${key}" << EOF
  15. ${value}
  16. EOF
  17.  
  18. }
  19. # Using the set command to "parse" the variables string
  20. set ${FOO}
  21. while [ "$1" ] ; do
  22. define "$1"
  23. shift
  24. done
  25. echo "var_with_two_words=${var_with_two_words}"
  26. echo "other_var=${other_var}"
  27.  
Success #stdin #stdout #stderr 0.01s 5388KB
stdin
Standard input is empty
stdout
var_with_two_words=foo
other_var=baz
stderr
./prog.sh: line 14: read: `bar'': not a valid identifier