Ask Your Question
0

What is the Difference of "source script.sh" and "./script.sh" in Bash?

asked 2023-04-04 23:23:15 +0000

bukephalos gravatar image

updated 2023-04-07 18:36:09 +0000

qstack gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2023-04-04 23:24:06 +0000

bukephalos gravatar image

When you run "./script.sh", you are telling the shell to execute the script as a separate process. The shell creates a new process to run the script, and any changes made to the environment (such as setting environment variables) by the script are only visible within that new process. The script is executed in a sub-shell and any changes made in the script to the environment variables, current working directory, etc. will not affect the parent shell that executed the script.

On the other hand, when you run "source script.sh", the script is executed within the current shell process, and any changes made to the environment by the script affect the current shell. The script is executed as if its commands were typed directly into the shell. This means that any changes made to environment variables, current working directory, etc. by the script will affect the parent shell that executed the "source" command.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

1 follower

Stats

Asked: 2023-04-04 23:23:15 +0000

Seen: 20 times

Last updated: Apr 04 '23