Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question

marked as duplicate by manatwork, rahmu, Ulrich Dangel, Renan, Gilles Aug 31 '12 at 23:16

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

up vote 4 down vote accepted

Just export it:


export MYVAR=myvalue
sh -c 'some_code_here'
share|improve this answer
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"
share|improve this answer