Fork me on GitHub

Project Notes

#033 Parsing Environment Settings

Techniques for parsing environment variables with bash built-in features only.

Notes

In a script, we may want to parse an environment variable and do something with the resulting parts.

If the structure is known ahead of time, Parameter Substitution may be all that is required.

If the structure is variable, then we can parse the variable into an Array by setting the appropriate field separator (IFS).

Test Script

The parse_env_setting.sh script demonstrates parsing an invented syntax from an environment variable.

It uses both IFS and parameter substitution techniques.

$ ./parse_env_setting.sh

This script demonstrates parsing an environment variable
Uses an invented encoding:
 - major parts delimited by /
 - minor parts delimited by :

parse_with_ifs: part1:part2
Major parts count: 1
loop with major_part==part1:part2
  loop with minor_part==part1
  loop with minor_part==part2

parse_with_ifs: part1:part2/part3:part4/part5:part6
Major parts count: 3
loop with major_part==part1:part2
  loop with minor_part==part1
  loop with minor_part==part2
loop with major_part==part3:part4
  loop with minor_part==part3
  loop with minor_part==part4
loop with major_part==part5:part6
  loop with minor_part==part5
  loop with minor_part==part6

parse_with_ifs_and_parameter_substitution: part1:part2
Major parts count: 1
loop with major_part==part1:part2
  first_part==part1
  second_part==part2

parse_with_ifs_and_parameter_substitution: part1:part2/part3:part4/part5:part6
Major parts count: 3
loop with major_part==part1:part2
  first_part==part1
  second_part==part2
loop with major_part==part3:part4
  first_part==part3
  second_part==part4
loop with major_part==part5:part6
  first_part==part5
  second_part==part6

Credits and References

About LCK#33 Bash
Project Source on GitHub Return to the Project Catalog

LittleCodingKata is my collection of programming exercises, research and code toys broadly spanning things that relate to programming and software development (languages, frameworks and tools).

These range from the trivial to the complex and serious. Many are inspired by existing work and I'll note credits and references where applicable. The focus is quite scattered, as I variously work on things new and important in the moment, or go back to revisit things from the past.

This is primarily a personal collection for my own edification and learning, but anyone who stumbles by is welcome to borrow, steal or reference the work here. And if you spot errors or issues I'd really appreciate some feedback - create an issue, send me an email or even send a pull-request.

LittleArduinoProjects LittleModelArt More on my blog