0

I have a bunch of piped shell commands that give some env output. I want to set these as env variables for an additional command to add to the chain

Kevins-MBP:ops kevzettler$ eb printenv | tail -n +2 | sed "s/ //g"
NODE_ENV=staging
RDSPassword=changme
RDSHost=sa1c7quehy7pes5.lolol.us-east-1.rds.amazonaws.com
RDSUsername=derp
1

1 Answer 1

5

You probably want:

source <(eb printenv | tail -n +2 | sed 's/ //g; s/^/export /')
your_next_command_that_uses_those_env_vars

A test:

  • define a function that prints out your sample variable definitions

    function eb {
    echo "
    NODE_ENV=staging
    RDSPassword=changme
    RDSHost=sa1c7quehy7pes5.lolol.us-east-1.rds.amazonaws.com
    RDSUsername=derp"
    }
    
  • call it to see what the pipeline produces

    $ eb printenv | tail -n +2 | sed 's/ //g; s/^/export /'
    export NODE_ENV=staging
    export RDSPassword=changme
    export RDSHost=sa1c7quehy7pes5.lolol.us-east-1.rds.amazonaws.com
    export RDSUsername=derp
    
  • source that output, test the current shell and a new shell to see if it's exported

    $ source <(eb printenv | tail -n +2 | sed 's/ //g; s/^/export /')
    $ echo $NODE_ENV
    staging
    $ sh -c 'echo $NODE_ENV'
    staging
    
4
  • Doesn't work. source <(eb printenv | tail -n +2 | sed "s/ //g"); echo $NODE_ENV gives me no output
    – kevzettler
    Mar 28, 2016 at 19:12
  • "works for me" (TM) -- break your pipeline down command by command to see where it breaks down. Mar 28, 2016 at 19:37
  • 1
    @kevzettler, you missed trailing sed command argument, substitutng beginning of each line with export, glenn jackmann's example does have it. It makes all your var assignments prepended with export directive, which is important
    – Tagwint
    Mar 28, 2016 at 19:41
  • @Tagwint he updated the post after I made that comment with the export directives
    – kevzettler
    Mar 28, 2016 at 20:22

Not the answer you're looking for? Browse other questions tagged .