Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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):

script.sh:

#!/bin/bash
#......
MYVAR=myvalue
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.

2 Answers 2

up vote 4 down vote accepted

Just export it:

#!/bin/bash

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