Possible Duplicate:
How to “send” variable to sub-shell?

I have a bash script and inside that script I do sh. How to pass a variable to the new shell? This is how it is (simplified to the specific problem):


sh -c 'some_code_here'

Basically I want to use the MYVAR in my some_code_here part. I am thinking of creating a temp script and executing it. But if there's a simple way to pass, I can avoid temp script.


Just export it:


export MYVAR=myvalue
sh -c 'some_code_here'
  • Thx. But export didn't work as I do sudo and run the script in the background(&) which goes into another shell. My mistake, I should have given the whole picture. Anyway, one other answer from the dup link posted worked. – user17080 Aug 31 '12 at 10:45

To avoid needing special shell syntax and avoid problems with things like sudo, use env:

env MYVAR=value sh -c "blah"