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.
./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 tosource
, 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 thebash
man page: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 tosource 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 viaexec
). 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../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.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):
help source
yields: