微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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,而不是当前目录.

原文地址:https://www.jb51.cc/linux/393249.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐