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

前端之家收集整理的这篇文章主要介绍了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.f2er.com/linux/393249.html

猜你在找的Linux相关文章