Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Parameter expansion in bash for loop to retreive key and value

I’m not sure what is doing this for loop in Bash that is using this kind of parameter expansions var_key="${env%%=*}"

    for env in "${envvars[@]}"; do
        var_key="${env%%=*}"
        var_value="${env#*=}"
        if [[ -z ${!var_key} ]]; then
            echo "export ${env}" >> "${FILE}"
            echo "export ${var_key}=\"${var_value}\"" >> "${FILE}"
        fi
    done

Any idea of what is being doing this for loop.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

"${env%%=*}" is one possible form of a "parameter expansion". It removes the longest match of the pattern =* from the value of the variable $env.
# is similar but operates on the beginning of the value.

E.g.

env=x=3=6
echo ${env%%=*}  # x
echo ${env%=*}   # x=3
echo ${env#*=})  # 3=6
echo ${env##*=}) # 6
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading