bash 在 sh shell 中找不到 source 命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13702425/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
source command not found in sh shell
提问by Milad
I have a script that uses sh
shell. I get an error in the line that uses the source
command. It seems source
is not included in my sh
shell.
我有一个使用sh
shell的脚本。我在使用该source
命令的行中收到错误消息。它似乎source
不包含在我的sh
外壳中。
If I explicitly try to run source
from shell I get:
如果我明确尝试source
从 shell运行,我会得到:
sh: 1: source: not found
Should I somehow install "source"? Do I have a wrong version of sh
?
我应该以某种方式安装“源”吗?我的版本不对sh
吗?
采纳答案by choroba
/bin/sh
is usually some other shell trying to mimic The Shell. Many distributions use /bin/bash
for sh
, it supports source
. On Ubuntu, though, /bin/dash
is used which does not support source
. Most shells use .
instead of source
. If you cannot edit the script, try to change the shell which runs it.
/bin/sh
通常是其他一些试图模仿 The Shell 的 shell。许多发行版使用/bin/bash
for sh
,它支持source
. 但是,在 Ubuntu/bin/dash
上使用不支持source
. 大多数 shell 使用.
而不是source
. 如果您无法编辑脚本,请尝试更改运行它的 shell。
回答by Guru
In Bourne shell(sh), use the . command to source a file
在 Bourne shell(sh) 中,使用 . 获取文件的命令
. filename
回答by shlsy
$ls -l `which sh`
/bin/sh -> dash
$sudo dpkg-reconfigure dash #Select "no" when you're asked
[...]
$ls -l `which sh`
/bin/sh -> bash
Then it will be OK
然后就OK了
回答by Travis Clarke
The source
builtin is a bashism. Write this simply as .
instead.
该source
内置是bashism。把这个简单地写成.
。
e.g.
例如
. $FILE
# OR you may need to use a relative path (such as in an `npm` script):
. ./$FILE
回答by Mojtaba Yousefi
This problem happens because jenkins Execute Shell runs the script via its /bin/sh
发生此问题是因为 jenkins Execute Shell 通过其 /bin/sh 运行脚本
Consequently, /bin/sh does not know "source"
因此, /bin/sh 不知道“来源”
You just need to add the below line at the top of your Execute Shell in jenkins
您只需要在 jenkins 中的 Execute Shell 顶部添加以下行
#!/bin/bash
回答by mah
The source
command is built into some shells. If you have a script, it should specify what shell to use on the first line, such as:
该source
命令内置于某些 shell 中。如果您有脚本,则应在第一行指定要使用的 shell,例如:
#!/bin/bash
回答by GangaRam Dewasi
I faced this error while i was trying to call sourcecommand from #Jenkins execute shell.
我在尝试从 #Jenkins 执行 shell调用源命令时遇到了这个错误。
source profile.txt
or
source profile.properties
source profile.txt
或者
source profile.properties
Replacement for sourcecommand is to use,
替换源命令是使用,
. ./profile.txt
or
. ./profile.properties
. ./profile.txt
或者
. ./profile.properties
Note:There is a space between the two dots(.)
注意:两个点之间有一个空格(.)
回答by DInfo2019
On Ubuntu, instead of using sh scriptname.sh to run the file, I've used . scriptname.sh and it worked!
The first line of my file contains:
#!/bin/bash
在 Ubuntu 上,我没有使用 sh scriptname.sh 来运行文件,而是使用了 . scriptname.sh,它奏效了!我的文件的第一行包含:
#!/bin/bash
回答by Gord Wait
I found in a gnu Makefile on Ubuntu, (where /bin/sh -> bash)
我在 Ubuntu 上的 gnu Makefile 中找到了(其中 /bin/sh -> bash)
I needed to use the . command, as well as specify the target script with a ./ prefix (see example below)
我需要使用 . 命令,以及使用 ./ 前缀指定目标脚本(参见下面的示例)
source did not work in this instance, not sure why since it should be calling /bin/bash..
source 在这种情况下不起作用,不知道为什么,因为它应该调用 /bin/bash ..
My SHELL environment variable is also set to /bin/bash
我的 SHELL 环境变量也设置为 /bin/bash
test:
$(shell . ./my_script)
Note this sample does not include the tab character; had to format for stack exchange.
请注意,此示例不包含制表符;必须格式化以进行堆栈交换。
回答by swati jain
sourceis a bash built-in command so to execute source command, you can log in as Root.
source是 bash 内置命令,因此要执行 source 命令,您可以以 Root 身份登录。
sudo -s
source ./filename.sh
sudo -s
source ./filename.sh