Search code examples
awksedgrep

Linux: Set Variable to Value following regex pattern, without quotes, in a single command (no pipes) in most compatible way possible


I need to grab a value from a config file (config.toml) and store it to a system variable.

I prefer to use a single command without pipes in the most compatible way possible (across linux systems)

Should return https://www.example.com with the config file set any of these ways:

base_url="https://www.example.com"
base_url = "https://www.example.com"
base_url  =  "https://www.example.com"

The solution I have so far based on some research is the following:

baseurl="$(sed -n -E 's/^base_url.*=\s+?\"//p' config.toml)"
echo $baseurl

My solution still has the trailing quotation mark, I have not yet figured out how to deal with it.

The other problem is I am not certain that this is the most universal solution. I prefer to use something that will work universally on most linux systems.

Appreciate all feedback, Thank You!


Solution

  • Different implementations of sed understand different types of regex. Only BRE is portable:

    sed -n 's/^base_url[[:space:]]*=[[:space:]]*"\([^"]*\)".*/\1/p' config.toml