3

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.

Hauke Laging
  • 90,279
  • Also see http://unix.stackexchange.com/questions/65634/difference-between-myscript-and-myscript – devnull May 18 '14 at 13:08
  • And http://unix.stackexchange.com/questions/43882/using-to-execute-files-in-bash – devnull May 18 '14 at 13:09
  • Maybe this too http://unix.stackexchange.com/questions/98795/whys-is-there-extra-dot-at-the-start-of-some-command-im-supposed-to-execute – devnull May 18 '14 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 '14 at 13:15

1 Answers1

7

./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.)

michas
  • 21,510
  • 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 '14 at 01: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 '14 at 05:13