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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 23:03:02  来源:igfitidea点击:

source command not found in sh shell

bashshellsh

提问by Milad

I have a script that uses shshell. I get an error in the line that uses the sourcecommand. It seems sourceis not included in my shshell.

我有一个使用shshell的脚本。我在使用该source命令的行中收到错误消息。它似乎source不包含在我的sh外壳中。

If I explicitly try to run sourcefrom 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/shis usually some other shell trying to mimic The Shell. Many distributions use /bin/bashfor sh, it supports source. On Ubuntu, though, /bin/dashis 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/bashfor 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 sourcebuiltin 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

https://wiki.ubuntu.com/DashAsBinSh#source

https://wiki.ubuntu.com/DashAsBinSh#source

回答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 sourcecommand 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.txtor source profile.properties

source profile.txt或者 source profile.properties

Replacement for sourcecommand is to use,

替换命令是使用,

. ./profile.txtor . ./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