我有一个使用sh
shell的脚本。在使用source
命令的那一行出现了错误,似乎我的sh
shell中没有包含source
命令。
如果我在shell中明确尝试运行source
,我会得到以下错误:
sh: 1: source: not found
我应该以某种方式安装“源代码”吗? 我的sh
版本有问题吗?
/bin/sh
通常是其他外壳程序模仿Shell。很多发行版使用/bin/bash
作为sh
,它支持source
。然而,在Ubuntu上,使用/bin/dash
,它不支持source
。大多数外壳程序使用.
代替source
。如果您无法编辑脚本,请尝试更改运行它的外壳程序。
SHELL := /bin/bash
。 - fIwJlxSzApHEZIl. filename
在某些操作系统/环境(例如Mac OS、Travis-CI、Ubuntu等)中,这必须是:. ./filename
. ./filename
。否则,在 Travis-CI 上运行测试时,我会收到“sh: 1: .: filename: not found”的错误提示。 - Adrien Joly$ls -l `which sh`
/bin/sh -> dash
$sudo dpkg-reconfigure dash #Select "no" when you're asked
[...]
$ls -l `which sh`
/bin/sh -> bash
那么就没问题了。
dpkg-reconfigure
可能无法访问所有用户,特别是在不基于Debian的系统上。 - Nico Haasesource
内置命令是 Bash 才有的。可以用简单的 .
替代它。
例如:. $FILE
# OR you may need to use a relative path (such as in an `npm` script):
. ./$FILE
https://wiki.ubuntu.com/DashAsBinSh#source
这个问题是因为Jenkins Execute Shell通过其/bin/sh运行脚本所致。
因此,/bin/sh不知道"source"命令。
您只需在Jenkins中的Execute Shell顶部添加以下行:
#!/bin/bash
我在尝试从 #Jenkins 执行 shell 时调用 source 命令时遇到了这个错误。
source profile.txt
或者
source profile.properties
source 命令的替代方法是使用,
. ./profile.txt
或者
. ./profile.properties
注意:两个点(.)之间有一个空格。
source
命令是一些shell内置的命令。如果您有一个脚本,它应该在第一行指定要使用的Shell,例如:
#!/bin/bash
source 是一个 Bash 内置命令,因此要执行 source 命令,您可以作为 Root 登录。
sudo -s
source ./filename.sh
你有两个选择,从纯shell切换到bash,这将启用bash功能,包括source。 https://superuser.com/questions/1220159/bash-prompt-variables-not-working/1764353#1764353
或者使用以下形式的"."代替source
. ~/.your_file_name
例如
. ~/.bashrc
请注意Dot和Tilda之间有一个空格。Tilda指的是主目录,如果您的文件位于主目录上方的某个位置,请使用绝对地址从/开始。
如果您无法更改脚本以使用"."代替"source",请将"sh"的链接更改为指向"bash"而不是"dash":
# which sh
/usr/bin/sh
# which bash
/usr/bin/bash
# ls -la /usr/bin/sh
lrwxrwxrwx 1 root root 4 Oct 5 15:55 /usr/bin/sh -> dash
# ln -sf /usr/bin/bash /usr/bin/sh
# ls -la /usr/bin/sh
lrwxrwxrwx 1 root root 4 Feb 6 09:18 /usr/bin/sh -> bash
source: not found
表示source
命令已经被正确执行,但它应该读取的文件不存在。 - Simon Richter