Doppler in script shebang

It would be great to be able to use doppler in the script shebang. On MacOS, this actually works

#!/usr/bin/env doppler run -- /bin/bash

But then you get a nasty surprise on linux—no support for shebang args! So then you have to do this:

#!/bin/bash
set -euo pipefail
quoted_args="$(printf "${1+ %q}" "$@")"
bash_source="${BASH_SOURCE:-$0}"
bash_script="$(tail -n +9 $bash_source)"
bash_script_with_variables="bash_source=\"$bash_source\""$'\n'"quoted_args=\"$quoted_args\""$'\n'"$bash_script"
doppler run --command="/bin/bash -c '$bash_script_with_variables'"
exit 0

# workaround for #!/usr/bin/env doppler run -- /bin/bash
# - no arguments on linux https://stackoverflow.com/a/4304187
# - fail on error "set -euo pipefail"
# - quoted args https://superuser.com/a/403369
# - skip lines https://stackoverflow.com/a/604871
# - bash source https://stackoverflow.com/a/63072463
# - concat variables with newline https://stackoverflow.com/a/21145867
# - bash -c https://stackoverflow.com/a/20858440

It’s long and ugly, but it works. I’d love for doppler to have a bin script that is basically just bash with the right env.

Hi there!

You might find this helpful:

Essentially, you do this:

set -a
source <(doppler secrets download --no-file --format env)
set +a

set -a causes all the environment variables set to be exported. Putting that at the top of your script should populate all your variables without having things get too messy for you!

1 Like

Hmm, that doesn’t seem to be working

Found a workaround

Ah, did you end up doing something like this?

export $(doppler secrets download --no-file --format env-no-quotes)

I think that might accomplish what you’re after too.

Kinda, ended up with:

export $(doppler secrets download --no-file --format env | xargs)

One caveat to the export $(...) form is that it will fail if there is a space in a secret’s value.

Quite a caveat! That’s what I experienced