Access a variable in a bash script


Question

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
14
5/2/2009 10:13:26 PM

Accepted Answer

Try

export myPath=/home/user/dir
9
5/2/2009 10:18:06 PM

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 $myPath

you'll get the desired results.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon