linux – “. filename“从/ bin / sh运行时找不到文件,从/ bin / bash工作;为什么?

我的简单脚本是这样的:
#!/bin/sh


DEF=.file_name_with_a_leading_dot.sh

. ${DEF}

注意顶行的/ bin / sh.
当我运行这个简单的脚本时,我收到一个错误,找不到该文件.
但是,如果我将该顶行更改为#!/ bin / bash,则脚本会在当前目录中找到该文件.

但是,在我的Ubuntu linux笔记本电脑上,我看到/ bin / sh是/ bin / bash的符号链接.那么,为什么我的脚本表现不同?

另外,我可以像这样运行脚本:

/bin/bash ./script.sh

没关系.

那么,我错过了什么?

解决方法

Manpage

. 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. If filename
does not contain a slash,file names in PATH are used to find the
directory containing filename.
The file searched for in PATH need not
be executable. When bash is not in posix mode,the current directory
is searched if no file is found in PATH.
If the sourcepath option to
the shopt builtin command is turned off,the PATH is not searched. If
any arguments are supplied,they become the positional parameters when
filename is executed. Otherwise the positional parameters are
unchanged. The return status is the status of the last command exited
within the script (0 if no commands are executed),and false if
filename is not found or cannot be read.

所以,似乎shebang#!/ bin / sh将你的bash设置为posix模式.在此模式下,仅评估PATH,而不是当前目录.

相关文章

1、安装Apache。 1)执行如下命令,安装Apache服务及其扩展包...
一、先说一下用ansible批量采集机器信息的实现办法: 1、先把...
安装配置 1. 安装vsftpd 检查是否安装了vsftpd # rpm -qa | ...
如何抑制stable_secret读取关键的“net.ipv6.conf.all.stabl...
1 删除0字节文件 find -type f -size 0 -exec rm -rf {} ...
## 步骤 1:安装必要的软件包 首先,需要确保系统已安装 `dh...