What's the difference between: “. [script]” or “source [script]”, “bash [script] or $SHELL [script]”, and “./ [script]” or “[script]”?

前端 未结 1 1702
暖寄归人
暖寄归人 2020-12-28 20:56

I know that source and . do the same thing, and I would be surprised to learn if the other pairs of commands in the title don\'t so the same thing

相关标签:
1条回答
  • 2020-12-28 21:37

    . script and source script execute the contents of script in the current environment, i.e. without creating a subshell. On the upside this allows script to affect the current environment, for example changing environment variables or changing the current work directory. On the downside this allows script to affect the current environment, which is a potential security hazard.

    bash script passes script to the bash interpreter to execute. Whatever shebang is given by script itself is ignored. ("Shebang" referring to the first line of script, which could e.g. read #!/bin/bash, or #!/usr/bin/perl, or #!/usr/bin/awk, to specify the interpreter to be used.)

    $SHELL script passes script to whatever is your current shell interpreter to execute. That may, or may not, be bash. (The environment variable SHELL holds the name of your current shell interpreter. $SHELL, if running bash, is evaluated to /bin/bash, with the effect detailed in the previous paragraph.)

    ./script executes the contents of a file script in the current work directory. If there is no such file, an error is generated. The contents of $PATH have no effect on what happens.

    script looks for a file script in the directories listed in $PATH, which may or may not include the current work directory. The first script found in this list of directories is executed, which may or may not be the one in your current work directory.

    0 讨论(0)
提交回复
热议问题