0

I'm trying to run a script and bash export is outputting text when I don't want it to because it's breaking up the output. I need to run a script which extracts some information and then inserts it into the next commands environment, kind of like acquiring the AWS secrets for the awscli and transparently passing them into the aws environment. I'm getting inconsistent results and I'm unsure why

$ ./bin/aws-creds mock
AWS_ACCESS_KEY_ID=mock1234
AWS_SECRET_ACCESS_KEY=mock1234
AWS_CREDS=success

$ ddt aws-creds mock
AWS_ACCESS_KEY_ID=mock1234
AWS_SECRET_ACCESS_KEY=mock1234
AWS_CREDS=success

Both output equally, which is great, so lets try to run them and use export on the output

$ export $(./bin/aws-creds mock)
$ export $(ddt aws-creds mock)
AWS_ACCESS_KEY_ID=mock1234
AWS_SECRET_ACCESS_KEY=mock1234
AWS_CREDS=success
declare -x .... a bunch of extra things from my environment

Wait a second? Both commands, when put into export using a subshell $(...) seem to do different things and this is what I want to fix. I want like the first ./bin script, no output, but all the new parameters inserted into the shells environment. It seems like the script I wrote 'ddt', doesn't work the same way for some reason.

Can anybody explain why and maybe suggest some way to fix it?

  • 1
    `ddt` may be sending its output to stderr rather than stdout; see ["Output not captured in bash variable" on Stackoverflow](https://stackoverflow.com/questions/37115949/output-not-captured-in-bash-variable). Warning: using a command's stderr as arguments to `export` may wind up trying to export actual error messages... which won't make any sense at all. – Gordon Davisson Mar 08 '22 at 19:17
  • You're absolutely right Gordon, I really didn't see that mistake, thank you very much for helping me! Please put this as an answer so I can mark it as the right one! The 'ddt' command was misconfigured to use stderr because of a copy/paste mistake, outputting to stdout worked – Christopher Thomas Mar 08 '22 at 20:10

1 Answers1

1

[Expanded from comments:] The ddt command was sending its output to standard error (stderr) rather than standard output (stdout), and $( ) only captures stdout. There are basically two ways to fix this:

  1. The better option is to fix ddt so it sends the variable values to stdout rather than stderr.
  2. If that's not possible, you can add 2>&1 after the ddt command, to redirect its stderr to stdout (and allow $( ) to capture it). But this runs the risk that if ddt prints any actual errors, it'll go ahead and try to export those messages, with unpredictable (and probably undesirable) results.

See the question "Output not captured in bash variable" on Stackoverflow.

Gordon Davisson
  • 11,036
  • 3
  • 27
  • 33