bashenvironment-variableslastpass

when exporting environment variables with spaces in value using lastpass bash script, receiving `not a valid identifier`


I'm using lpass-env which uses the lpass cli to pull down a note from my vault and set them as environment variables.

The note looks similar to this:

VARIABLE_WITH_SPACES="abcd 1234"
VARIABLE_WITHOUT_SPACES=abcd1234

When the script runs the following command:

$(lpass show --notes mynote | awk '{ print "export", $0 }')

I get the following output:

-bash: export: `1234"': not a valid identifier

So it doesn't like the spaces in the value. How can I fix this?

The same thing happens if you cat a file that has spaces in some of the values:

$(cat file | awk '{ print "export", $0 }')
-bash: export: `1234"': not a valid identifier

Solution

  • Word splitting shell expansion just scans the result of command substitutions - the quotes that come after expansion of a command substitution are not special and become part of the parameters.

    You could (ab-)use the source command and pass a process substitution:

    . <(lpass show --notes mynote | sed 's/^/export /')
    

    or be evil:

    eval "$(lpass show --notes mynote | sed 's/^/export /')"