What is the difference between these two commands which are used to run shell script?

前端 未结 4 1668
误落风尘
误落风尘 2021-01-25 04:16

Here I have one script which exporting some necessary path in Linux. After running this script I have to run some other scripts.

I have two scripts

1 imp         


        
相关标签:
4条回答
  • 2021-01-25 04:30

    ./import.sh executes the shell script in a new sub shell shell.

    . ./import.sh executes the shell script in the current shell.
    The extra . denotes the current shell.

    0 讨论(0)
  • 2021-01-25 04:35

    The difference between the two invocations is that ./import.sh is executing import.sh as a program, and . ./import.sh is evaluating it in your shell.

    If "import.sh" were an ELF program (a compiled binary, not a shell script), . ./import.sh would not work.

    If import.sh had a shebang at the top (like #!/bin/perl), you'd be in for a nasty surprise and a huge number of error messages if you tried to do . ./import.sh - unless the shebang happened to match your current shell, in which case it would accidentally work. Or if the Perl code were to somehow be a valid Bash script, which seems unlikely.

    . ./import.sh is equivalent to source import.sh, and doesn't require that the file have the execute bit set (since it's interpreted by your already-running shell instead of spawned via exec). I assume this is the source of your error. Another difference is that ./import.sh runs in the current shell instead of a subshell, so any non-exported environment variables will affect the shell you used for the launch!

    So, they're actually rather different. You usually want to ./import.sh unless you know what you're doing and understand the difference.

    0 讨论(0)
  • 2021-01-25 04:38

    ./import.sh runs the script as a normal script - that is, in a subshell. That means it can't affect your current shell in any way. The paths it's supposed to import won't get set up in your current shell.

    The extra ., which is equivalent to source, runs the script in the context of your current shell - meaning it can modify environment variables, etc. (like the paths you're trying to set up) in the current shell. From the bash man page:

    . filename [arguments]
    source filename [arguments]
    Read and execute commands from filename in the current shell environment and return the exit status of the last command executed from filename.

    0 讨论(0)
  • 2021-01-25 04:51

    The . ./import.sh "sources" the script, where as simply ./import.sh just executes it.

    The former allows you to modify the current environment, where the later will only affect the environment within the child execution.

    The former is also equivalent to (though mostly Bash-specific):

    source ./import.sh
    

    help source yields:

    source: source filename [arguments]

    Execute commands from a file in the current shell.

    Read and execute commands from FILENAME in the current shell. The entries in $PATH are used to find the directory containing FILENAME. If any ARGUMENTS are supplied, they become the positional parameters when FILENAME is executed.

    Exit Status: Returns the status of the last command executed in FILENAME; fails if FILENAME cannot be read.

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