What is the difference between these two commands

2020-04-23 07:06发布

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 import.sh =  importing paths 
2 main.sh   = this script do something with HCI (use for Bluetooth purpose).

when I run ./import.sh and than ./main.sh then it's giving error. And when I run . ./import.sh and then ./main.sh then it's working fine.

So what is the diff between ./import.sh and . ./import.sh?

What happens if I run script as a super user? May be . ./ using for run script as a super user.

标签: linux shell
4条回答
我命由我不由天
2楼-- · 2020-04-23 07:15

./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.

查看更多
一夜七次
3楼-- · 2020-04-23 07:16

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.

查看更多
爷的心禁止访问
4楼-- · 2020-04-23 07:18

./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.

查看更多
手持菜刀,她持情操
5楼-- · 2020-04-23 07:30

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.

查看更多
登录 后发表回答