0

I have a command I execute in the bash which requires an environment variable. If I call it like this, everything works fine:

export MYVAR=value & my_first_command

But now I want to pipe the result to a second command, which requires the same environment variable. I tried this one:

export MYVAR=value & my_first_command | my_second_command

In that case, MYVAR seems not to be set for my_second_command. What's the correct syntax to make MYVAR available to my_second_command too?

5
  • 2
    export should make MYVAR avaliable to all subprocess. FWIW, you have a typo, try export MYVAR=value && my_first_command to execute my_first_command if the previous command was successful. Commented Nov 13, 2013 at 9:12
  • 1
    I don't even understand how the first attempt can work. Using &, the variable ought to be exported in its do-nothing subshell only, and not to my_first_command. Commented Nov 13, 2013 at 9:12
  • what's wrong with export MYVAR=value ; my_first_command ; my_second_command ? Commented Nov 13, 2013 at 9:14
  • @slayedbylucifer In this case, I think its expected to work. As it just sequence of commands, where ';' acts as cmd-seperator. With '|' it won't work as per original problem statement above Commented Nov 13, 2013 at 10:19
  • Scope isn't quite the right concept here. That would imply that MYVAR isn't defined in the child, but that it could then somehow look up to the parent to see if MYVAR is defined there. Instead, the child is given its own copy of MYVAR when it begins, which is not affected even if the parent changes its value of MYVAR while the child is still running. Commented Nov 13, 2013 at 12:44

1 Answer 1

2

The & is incorrect and should be ; instead.

Using & is syntactically valid, and runs a command in the background; but running export as a background task makes no sense at all.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.