Difference between launching a script with ./script.sh and . ./script.sh

社会主义新天地 提交于 2020-01-09 05:19:45

问题


Please tell me what is the difference in bash shell between launching a script with ./script.sh and . ./script.sh?


回答1:


As klausbyskov says, the first form requries that the file have its executable permission bit set.

But more importantly, the first form executes the script in a separate process (distinct from, independent of, and unable to make changes in the shell that launched it). The second form causes the initial shell to directly run the commands from the file (as if you had typed them into the shell, or as if they were included in the script that does the ‘sourcing’).

A script that contains FOO=bar; export FOO will have not create an exported FOO environment variable in the shell that runs the first variant, but it will create such a variable in a shell that runs the second variant.

The second form (‘sourcing’) is a bit like a #include in C.




回答2:


The first requires the file to have the +x flag set. The second uses the . command aka "source", described here.



来源:https://stackoverflow.com/questions/1880735/difference-between-launching-a-script-with-script-sh-and-script-sh

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!