$ whatis source
source: nothing appropriate.
$ man source
No manual entry for source
$ source
bash: source: filename argument required
source: usage: source filename [arguments]


它存在并且可以运行。为什么在Ubuntu中没有关于它的任何文档?
它是做什么的?如何安装有关它的文档?

评论

相关:superuser.com/questions/176783/…

您忘记了$ type源source是内置的shell

我的shell返回了此$ whatis源代码源(1)-bash内置命令,请参见bash(1)。此外,手册源将我带到BASH_BUILTINS(1)手册页。顺便说一句,这是在Fedora上,不知道为什么这些debian软件包没有(或不良记录)。

@lesmana,很棒的链接。链接的答案是对该问题的更彻底的答案。

尝试“帮助源”

#1 楼

source是bash shell内置命令,用于在当前shell中执行作为参数传递的文件的内容。它在.(句点)中有一个同义词。


语法

. filename [arguments]

source filename [arguments]



评论


source是bash专用命令,还是其他shell也有呢? (我要求在问题上正确添加标签...)

– Jonik
09年9月24日在11:17

Afaik,来源存在于伯恩贝壳中,因此可能存在于其所有后代中。 zh.wikipedia.org/wiki/Bourne_shell。我知道不是所有的shell都具有source命令,而不太确定哪些shell包含它。

–user4358
09年9月24日在11:47

@ nagul,Bourne外壳程序中没有源代码,它是GNU扩展,后来出现了。原始且仍可移植的语法(POSIX)使用“点”命令,即代替。考虑到它的输入时间更长并且没有附加值,我个人不使用源代码。我想它的主要目的是使脚本对新手更具可读性。

– jlliagre
13年8月2日在14:02

@jlliagre我个人的“解释为什么有来源”的意思是,来源不仅更具描述性,而且看起来像是错别字。当我通过电子邮件发送技术命令时,有人跳过了句点/点。

– Rich Homolka
2014年10月10日18:31

此命令的一种常见用法是使Shell脚本在“配置文件”中获取源代码,该文件主要包含变量分配。然后,变量分配控制脚本其余部分的工作。当然,一个好的脚本会在源代码之前将变量设置为合理的默认值,或者至少检查有效值。

–LawrenceC
2014年10月10日18:41

#2 楼

小心! ./source不太相同。



./script将脚本作为可执行文件运行,并启动一个新的shell来运行它

source script在当前Shell环境中从文件名读取并执行命令

注意:./script不是. script,而是. script == source script

https://askubuntu.com/questions/182012 / bash与所有来源之间是否存在差异?lq = 1

评论


您正在混用./command和。脚本。 source-command与.-command相同。使用./meh表示在当前目录中运行名为meh的脚本/二进制文件,而与source /无关。 -命令。如您的链接中的回答所述。

–约翰·埃洛夫森(Joakim Elofsson)
13年8月1日在13:14

@JoakimElofsson在链接中已提及,但我将修改答案以避免误解。请更正。

– damphat
13年8月2日在11:45

接受的答案也指向这个答案很重要,因为有一段时间我认为./ == source ==。

–丹尼尔F
17年12月6日在12:51

#3 楼

了解'type'命令很有用:

> type source
source is a shell builtin


每当内置外壳时,就该做man bash了。

评论


读书的人总是知道一些新的东西)

–user83293
13年8月15日在8:41

您还可以使用帮助{buildin-name},即帮助源。

–LawrenceC
2014年10月10日18:39

帮助并非在所有地方都有效(至少在zsh中有效)。类型呢。

– kumarharsh
2014年3月28日上午8:35

放大:如果您正在使用bash,并且知道(也许通过'type')它是一个内置命令,那么'help'将使您直接进入所需的文档段落,而无需花掉4184行的' bash的文字。

–罗恩·伯克(Ron Burk)
15年4月29日在19:43

#4 楼

。 (一个句点)是bash shell内置命令,该命令在当前shell中执行作为参数传递的文件中的命令。 'source'是'。'的同义词。

来自Bash手册页:

. 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  exe‐
       cuted from filename.  If filename does not contain a slash, file
       names in PATH are used to find the  directory  containing  file‐
       name.   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 posi‐
       tional 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.


#5 楼

“源”是“”的长版本。命令。在bash提示符下,可以执行以下操作:

source ~/.bashrc


重新加载当前运行的bash的bash设置(已更改?)。

简短版本会be:

. ~/.bashrc


手册页:

. 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. 


#6 楼

source命令在当前shell环境中执行提供的脚本(不是必须具有可执行权限),而./命令在新的shell中执行提供的可执行脚本。

source命令的确具有同义词. filename

为了更加清楚,请看下面的脚本,该脚本设置了别名。

make_alias

#! /bin/bash

alias myproject='cd ~/Documents/Projects/2015/NewProject'


现在,我们有两个选择来执行此脚本。但是只有一个选项,可以在这两个选项之间创建当前shell所需的别名。

选项1:./make_alias


首先使脚本可执行。 br />
chmod +x make_alias


执行

./make_alias


验证

alias


输出

**nothing**


哇!新外壳程序不再使用Alias。

让我们选择第二个选项。

选项2:source make_alias


执行

source make_alias




. make_alias


验证

alias


>输出

alias myproject='cd ~/Documents/Projects/2015/NewProject'


是的,设置了别名。

#7 楼

如有疑问,最好的方法是使用info命令:

[root@abc ~]# info source

BASH BUILTIN COMMANDS
       Unless otherwise noted, each builtin command documented in this section
       as accepting options preceded by - accepts -- to signify the end of the
       options.   The  :, true, false, and test builtins do not accept options
       and do not treat -- specially.  The exit, logout, break, continue, let,
       and  shift builtins accept and process arguments beginning with - with-
       out requiring --.  Other builtins that accept  arguments  but  are  not
       specified  as accepting options interpret arguments beginning with - as
       invalid options and require -- to prevent this interpretation.
       : [arguments]
              No effect; the command does nothing beyond  expanding  arguments
              and  performing any specified redirections.  A zero exit code is
              returned.

        .  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 exe-
              cuted from filename.  If filename does not contain a slash, file
              names  in  PATH  are used to find the directory containing file-
              name.  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  posi-
              tional  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.


评论


您能提供的不仅仅是RTFM吗?

– Peter Mortensen
17年3月19日在9:04

#8 楼

在外壳程序中键入命令“ help source”。

您将得到如下输出:

source: source filename [arguments]

Execute commands from a file in the current shell.

Read and execute commands from FILENAME in the current shell.  The
entries in $PATH are used to find the directory containing FILENAME.
If any ARGUMENTS are supplied, they become the positional parameters
when FILENAME is executed.

Exit Status:
Returns the status of the last command executed in FILENAME; fails if
FILENAME cannot be read.


#9 楼

摘自Linux文档项目Advanced Bash脚本指南,
第15章-内部命令和内建程序:


源,。 (点命令):
从命令行调用此命令后,将执行脚本。在脚本中,源文件名将加载文件文件名。源文件(点命令)会将代码导入脚本,并附加到脚本(与C程序中的#include指令相同)。最终结果与脚本主体中实际存在“源”代码行相同。这在多个脚本使用公共数据文件或函数库的情况下很有用。
如果源文件本身是可执行脚本,则它将运行,然后将控制权返回给调用它的脚本。为此,源可执行脚本可以使用return。


因此,对于那些熟悉C编程语言的人来说,采购文件的效果类似于#include指令。

还请注意,您可以将位置参数传递给源文件,例如:

$ source $filename $arg1 arg2


评论


该答案与之前的9个答案有何不同?

–斯蒂芬·劳赫(Stephen Rauch)
17年6月12日在0:31

我添加了另一个信息源和之前未提及的其他信息。

–亚历山大·奥利维拉(Alexandro de Oliveira)
17年6月12日在1:16

我不知道源可以接受参数或使用return。

–乔
19年8月19日在20:51



#10 楼

应该注意的是,尽管source是一个很棒的命令,但是它的简写形式.都不会提供多个文件,这意味着

source *.sh




. script1.sh script2.sh


不起作用。

我们可以使用for循环进行回退,但是它将多次发出可执行文件,创建多个命令或发出可执行文件。

结论:source不会将多个文件作为输入。该参数必须为1。

糟透了恕我直言。

#11 楼

使用source,您可以将另一个文件中的变量或函数传递到脚本中并使用它们,而不必再次编写它们。

F.I:

#!/bin/bash

source /etc/environment

source /myscripts/jetty-common/config/jetty-functions.sh


干杯