Difference between ./myscript.sh and source myscript.sh(. myscript.sh)

When you run ./myscript.sh, a new child process of shell is created and script is run there. So, if you export any variables in the script, after the script completes execution, they don't exist.

Whereas when you run source myscript.sh or '. myscript.sh', it won't create a child process, instead it will run in the same environment, hence values of environmental variables persist.

The below is the screenshot of a simple script which exported a variable and the result is different when you run ./myscript.sh or source myscript.sh


Modified the script to print the PID and PPID of the script, with 'source myscript.sh', the PID of the shell script is the PID of the shell, whereas in './myscript.sh' it is different.



Comments

Popular posts from this blog

bb.utils.contains yocto

Difference between RDEPENDS and DEPENDS in Yocto

make config vs oldconfig vs defconfig vs menuconfig vs savedefconfig