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.

Please excuse me if I am not clear while asking questions. I will try my best to be very clear while asking questions.

While learning Embedded Linux, we are setting an environment for our project. We wrote a shell script to set the environment which is similar to this:

export PROJECT=example-sys
export PRJROOT=/home/karim/${PROJECT}
export TARGET=powerpc-linux
export PREFIX=${PRJROOT}/tools
export TARGET_PREFIX=${PREFIX}/${TARGET}
export PATH=${PREFIX}/bin:${PATH}
cd $PRJROOT

Saved the script as prjenv.sh. As I practiced running the scripts or any executable using ./xyz.sh or ./abc, I ran ./prjenv.sh and the shell did not complain but when I gave export -p to see if the variables were exported or not, I could not find them in the list.

Later I noticed, I was expected to use . prjenv.sh, after I ran the script, it set the environment variables.

May I know what is the difference between . and ./ and when to use which one?

Please let me know if you need more information.

PS: This is my first time working with Linux so I apologize if some of the technical terms I used are wrong.

share|improve this question

marked as duplicate by devnull, msw, Hauke Laging, slm, Braiam May 18 at 14:29

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.

    
Also see unix.stackexchange.com/questions/65634/… –  devnull May 18 at 13:08
    
    
Maybe this too unix.stackexchange.com/questions/98795/… –  devnull May 18 at 13:10
    
Your use of the terminology is fine. Your error is in not understanding what the almost invisibly named shell built-in command . is for. The references that @devnull listed will enlighten you. –  msw May 18 at 13:15
add comment

1 Answer 1

./script will run the file script located in the current directory . as a new process.

. script calls the shell function . (which is an alias to source) with the argument script. This is pretty much the same as entering all the contained lines in the current shell.

You cannot set variables using the first option as the variables are set only for the new process. Variables are only exported from parent to child not the other way around. (i.e. a called process cannot modify variables in the current process.)

share|improve this answer
    
Thank you for your quick reply. Its really helpful. One more quick question. Does it mean after running the script using "./script" if we close the parent process, those variables will be available after reopening another process? or it means Variables exported in child process are never available after child process is done executing the script? –  user3600064 May 19 at 1:22
    
A process is something running in memory in its own little world. On termination this whole world/memory is deleted, including all of the variables set into this world. - There are no global/persistent variables. If you need something like this you usually put them in some startup file like ~/.bashrc. In this case whenever a new bash is started it reads that file and sets the variables it its own world. If the variables are exported, those variables are available to any child, i.e. any command started by this shell. –  michas May 19 at 5:13
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.