views:

375

answers:

4

In the bash command line, I set a variable myPath=/home/user/dir . I created a script in which I put echo $myPath but it doesnt seem to work. It echoes nothing. What can I do to access the myPath variable in the script. If I write echo $myPath in the command, it works, but not in the script.

+1  A: 

Try

export myPath=/home/user/dir
David Zaslavsky
+3  A: 

how did you assign the variable? it should have been:

$ export myPath="/home/user/dir"

then inside a shell program like:

#!/usr/bin/env bash
echo $ppp

you'll get the desired results.

Richard
+1  A: 

You could also do this to set the myPath variable just for myscript

myPath="whatever" ./myscript

For details of the admitted tricky syntax for environment variables see: http://www.pixelbeat.org/docs/env.html

pixelbeat
A: 

You must declare your variable assignment with "export" as such:

export myPath="/home/user/dir"

This will cause the shell to include the variable in the environment of subprocesses it launches. By default, variables you declare (without "export") are not passed to a subprocess. That is why you did not initially get the result you expected.

digijock