Bash、Source、“.”和“./”执行的区别

系统 Linux
本文探讨了在 Linux 上运行 shell 命令的各种方法。

本文探讨了在 Linux 上运行 shell 命令的各种方法。


在Linux 上工作时,您可能会遇到一些未按您预期的方式执行的文件——例如,您可能在当前目录中有一个文件,但是当您输入它的名称时它没有运行。你得到file_name.sh command not found了,但实际上,文件在那里。该文件不起作用,即使具有执行权限,因为当你在 shell 上写一些东西并运行它时,你的$PATH变量会被检查。如果目录内有任何匹配的命令$PATH,例如/usr/bin,它将执行。除非有匹配的命令,否则您将收到错误消息。

因此,您需要解决文件的路径。让我们创建一个简单的Linux shell 脚本并对该脚本具有执行权限。以下示例是用Bash Shell编写的。

mkdir test && cd test
echo 'echo "hello world PID:$$ ParentPID:$PPID"' > test.sh
chmod 755 test.sh

创建脚本后,让我们按顺序执行它们。

壳1测试.sh2#test.sh:找不到命令34. 测试.sh5#hello world PID:19245 ParentPID:1924367. ./test.sh # 同上。它只是明确指定当前目录。8#hello world PID:19245
ParentPID:19243910./test.sh #hello world PID:23044 ParentPID:1924511bash test.sh #hello world PID:23045 ParentPID:19245

$PATH除非当前目录在环境变量中,否则直接给出文件名不起作用。当您使用POSIX标准 shell(如 ksh)时,第二个命令也会失败,因为在运行命令 shell 时检查/命令内部是否存在。如果有,那么它会查找您处理的当前工作目录或绝对路径。与此相反,然后它查看里面的命令$PATH。我当前正在处理的目录不在 PATH 内,因此会出现错误。

test.sh
#test.sh: command not found
. test.sh
#hello world PID:19245 ParentPID:19243
. ./test.sh # this is same with above. it is just specifies current directory explicitly.
#hello world PID:19245 ParentPID:19243
./test.sh #hello world PID:23044 ParentPID:19245
bash test.sh #hello world PID:23045 ParentPID:19245

假设您正在使用Bash。通过执行两者. file.sh或. ./file.sh结果来运行文件将是相同的,但不是./test.sh. source和bash命令呢?

如果您使用任何 shell 命令,例如 Bash 或 ksh,您将生成一个新的 shell 来运行该命令。因此,您设置的每个变量在新 shell 中都不可用。另一方面,source使用当前的 shell 并且不产生新的 shell 进程。因此,您在文件中所做的任何更改都会影响您当前的 shell。上面,正如您从输出中看到的那样,当您执行./或bash因为它们正在产生新进程时,PID 会发生变化。如上所示,命令的父进程 ID (PPID)bash test.sh等于命令的进程 ID (PID) . ./test.sh。

让我们设置一个变量并在test.sh脚本中打印它。

bash -posix
test.sh
#bash: test.sh: command not found
. test.sh
#bash: .: test.sh: file not found
. ./test.sh
#hello world PID:23493 ParentPID:19245
./test.sh
#hello world PID:23539 ParentPID:23493
bash test.sh
#hello world PID:23540 ParentPID:23493
exit

显然,该bash test.sh命令没有给出$STR变量输出,因为新的 shell 不知道。它没有设置在新外壳中。让我们在脚本中设置一个变量。

echo 'NEWSTR="WORLD"' >> test.sh
echo 'echo "NEWSTR is $NEWSTR"' >> test.sh
bash test.sh
#hello world PID:25318 ParentPID:19245
#STR is #NEWSTR is WORLD
echo $NEWSTR #this will give empty output
#
. test.sh
#hello world PID:19245 ParentPID:19243
#STR is HELLO #NEWSTR is WORLD
echo $NEWSTR
# WORLD
source test.sh
#hello world PID:19245 ParentPID:19243
# STR is # NEWSTR is WORLD
echo $NEWSTR
# WORLD

.并source在当前 shell 中运行,因此我们可以看到新变量。这就是运行该bash .bashrc命令不会更新您的 PATH 变量的原因。您应该使用source命令运行或使用.. 因此,您必须使用 source 命令来更改 PATH 变量。

最后,让我们尝试使用此信息来更改和设置 PATH 变量。

mkdir directory && cd directory
echo 'echo "FILE"' > file.sh && chmod 755 file.sh
echo 'echo "COMMAND"' > echocommand && chmod 755 echocommand
pwd
# /home/ofk/test/directory
cd
# change PATH variable inside your .profile (or where ever you set PATH) file and add above path
# PATH="$PATH:/home/ofk/test/directory"
bash .profile
# try to run echocommand or file.sh
echocommand
# echocommand: command not found
file.sh
# file.sh: command not found
source .profile
echocommand
# COMMAND
file.sh
# FILE

结论

./或shell命令(bash、ksh)启动新的 shell 并运行命令。

. file_name或source命令在当前 shell 上运行。

责任编辑:华轩 来源: 今日头条
相关推荐

2020-06-28 07:33:01

source命令BashLinux

2022-08-31 08:33:54

Bash操作系统Linux

2022-11-14 14:51:23

单引号双引号Linux

2023-08-29 09:50:42

Unix shellLinux

2019-03-08 11:00:32

BashLinux

2018-01-22 09:12:18

Linuxbashssh

2022-09-27 10:07:01

要使用 source

2014-09-26 15:41:51

2010-02-03 14:57:18

Linux shell

2021-08-04 08:33:59

TypeScriptConst Readonly

2021-03-11 21:30:43

BATSBash软件开发

2023-07-27 17:41:02

BashZshFish

2010-07-30 14:09:30

FlexFlash

2011-03-03 09:55:34

forwardredirect

2011-06-08 11:02:14

GetPost

2009-12-03 10:12:24

LinuxUnix

2018-11-14 09:46:33

DoSDDoS攻击

2020-08-12 23:21:49

平台即服务PaaSaPaaS

2020-07-09 10:53:05

MPLSIP网络

2024-04-24 11:31:52

Android移动开发
点赞
收藏

51CTO技术栈公众号