Was macht "Quelle"?

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

Es existiert und ist lauffähig. Warum gibt es in Ubuntu keine Dokumentation darüber? Was tut es? Wie kann ich eine Dokumentation dazu installieren?

511
verwandt: http://superuser.com/questions/176783/what-is-the-difference-zwischen-executing-a-bash-script-und-sourcing-a-bash-script/176788#176788 lesmana vor 13 Jahren 4
Sie haben "$ type source" vergessen. "source ist eine integrierte Shell" bnjmn vor 10 Jahren 45
Meine Shell gab diese `$ whatis source`` source (1) - bash-Befehle zurück, siehe bash (1) `. Außerdem führt mich "man source" zu den Manpages "BASH_BUILTINS (1)". Dies ist auf Fedora übrigens keine Idee, warum diese Debian-Pakete nicht (oder schlecht) dokumentiert sind. arielnmz vor 9 Jahren 1
@lesmana, großartiger Link. Diese [verlinkte Antwort] (http://superuser.com/a/176788/57649) ist die gründlichere Antwort auf diese Frage. Scott vor 9 Jahren 2
Versuchen Sie "Hilfequelle" Jasser vor 8 Jahren 1
`source --help` ist ein guter Anfang. Thorbjørn Ravn Andersen vor 6 Jahren 0

11 Antworten auf die Frage

420
nagul

source is a bash shell built-in command that executes the content of the file passed as argument, in the current shell. It has a synonym in . (period).

Syntax

. filename [arguments] source filename [arguments] 
Ist `source` ein bash-spezifischer Befehl oder haben es andere Shells auch? (Ich frage nach Tags, um die Frage zu beantworten ...) Jonik vor 14 Jahren 5
Afaik, "source", war in der Bourne-Shell präsent und daher wahrscheinlich in allen seinen Nachkommen vorhanden. http://en.wikipedia.org/wiki/Bourne_shell. Ich weiß, dass nicht alle Shells den 'source'-Befehl haben, weniger sicher, welche Shells ihn enthalten. nagul vor 14 Jahren 2
@nagul, `source` war nicht in der Bourne-Shell vorhanden, es ist eine GNU-Erweiterung, die viel später kam. Die ursprüngliche und noch tragbare Syntax (POSIX) verwendet stattdessen den Befehl "Punkt", dh `.`. Ich persönlich verwende niemals "source", da es länger zu tippen ist und keinen Mehrwert bietet. Ich denke, ihr Hauptzweck besteht darin, Skripte für Neulinge lesbarer zu machen. jlliagre vor 10 Jahren 11
@jlliagre mein persönliches "erklären warum Quelle haben" ist, dass "source" nicht nur beschreibender ist, sondern es sieht aus wie etwas anderes als ein Tippfehler. Ich habe die Leute / Benutzer die Punkt / Punkt überspringen lassen, wenn ich Tech-Befehle per E-Mail sende Rich Homolka vor 10 Jahren 16
Dieser Befehl wird häufig verwendet, wenn ein Shell-Skript in einer "Konfigurationsdatei", die hauptsächlich Variablenzuweisungen enthält, "source" verwendet. Die Variablenzuweisungen steuern dann die Funktionen des restlichen Skripts. Natürlich setzt ein gutes Skript Variablen vor der "Quelle" auf sinnvolle Standardwerte oder sucht zumindest nach gültigen Werten. LawrenceC vor 10 Jahren 3
Tatsächlich ist "." Der ursprüngliche Befehl und "source" ist das Synonym / Alias ​​dafür. helt vor 6 Jahren 1
241
damphat

Achtung! ./und sourcesind nicht ganz dasselbe .

  • ./scriptFührt das Skript als ausführbare Datei aus und startet eine neue Shell, um es auszuführen
  • source scriptliest und führt Befehle aus dem Dateinamen in der aktuellen Shell- Umgebung aus

Anmerkung: ./scriptist nicht . script, aber . script==source script

https://askubuntu.com/questions/182012/isthere-a-difference-zwischen-und-source-in-bash-nach-all?lq=1

You are mixing up ./command and . script. source-command is same as .-command. Using ./meh says run script/binary named meh in the current directory, and got nothing to do with source/. -command. As explained in answer in your link. Joakim Elofsson vor 10 Jahren 24
@JoakimElofsson Es wird im Link erwähnt, aber ich werde die Antwort ändern, um Missverständnisse zu vermeiden. Bitte korrigiere es. damphat vor 10 Jahren 2
Es ist irgendwie wichtig, dass die akzeptierte Antwort auch auf diese Antwort verweist, da ich für einen Moment dachte, dass ./ == source == Daniel F vor 6 Jahren 1
82
micans

Es ist nützlich, den Befehl 'type' zu kennen:

> type source source is a shell builtin 

Wann immer etwas in eine Shell eingebaut wird, ist es Zeit zu tun man bash.

Immer etwas Neues beim Lesen von "Mann" wissen) vor 10 Jahren 1
Sie können auch `help ` verwenden, also `help source`. LawrenceC vor 10 Jahren 17
`help` funktioniert nicht überall (zumindest in zsh). `type` tut. kumar_harsh vor 10 Jahren 1
Um dies zu verstärken: Wenn Sie bash verwenden und (vielleicht über 'type') wissen, dass es sich um einen integrierten Befehl handelt, gelangen Sie mit 'help' direkt zum gewünschten Abschnitt der Dokumentation, ohne durch 4.184 Zeilen von ' Mann bash 'Text. Ron Burk vor 9 Jahren 2
34
Jawa

. (a period) is a bash shell built-in command that executes the commands from a file passed as argument, in the current shell. 'source' is a synonym for '.'.

From Bash man page:

. 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. 
20
Joakim Elofsson

'source' is the long version of '.' command. On the bash prompt one can do:

source ~/.bashrc 

to reload your (changed?) bash setting for current running bash.

Short version would be:

. ~/.bashrc 

The man page:

. 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 short 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. 
Dies sollte die akzeptierte Antwort sein. Peter Mortensen vor 7 Jahren 0
18
Harsh Vakharia

sourceBefehl führt das bereitgestellte Skript (ausführbare Berechtigung ist nicht zwingend erforderlich ) in der aktuellen Shell-Umgebung aus, während ./das bereitgestellte ausführbare Skript in einer neuen Shell ausgeführt wird.

sourceBefehl haben ein Synonym . filename.

Um es klarer zu machen, werfen Sie einen Blick auf das folgende Skript, das den Alias ​​setzt.

make_alias

#! /bin/bash  alias myproject='cd ~/Documents/Projects/2015/NewProject' 

Jetzt haben wir zwei Möglichkeiten, dieses Skript auszuführen. Mit nur einer Option kann der gewünschte Alias ​​für die aktuelle Shell unter diesen beiden Optionen erstellt werden.

Option 1: ./make_alias

Machen Sie das Skript zuerst ausführbar.

chmod +x make_alias 

Ausführen

./make_alias 

Überprüfen

alias 

Ausgabe

**nothing** 

Hoppla! Alias ​​ist mit der neuen Shell weg.

Lass uns mit der zweiten Option gehen.

Option 2: source make_alias

Ausführen

source make_alias 

oder

. make_alias 

Überprüfen

alias 

Ausgabe

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

Ja, Alias ​​ist eingestellt.

5
Akshay Upadhyaya

Im Zweifelsfall verwenden Sie am besten den infoBefehl:

[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. 
Könnten Sie mehr als nur RTFM bereitstellen? Peter Mortensen vor 7 Jahren 0
3
Jasser

Geben Sie den Befehl "help source" in Ihre Shell ein.

Sie erhalten folgende Ausgabe:

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. 
2
Alexandro de Oliveira

Aus dem Linux-Dokumentationsprojekt, Advanced Bash Scripting Guide,
Kapitel 15 - Befehle und eingebaute Interna :

Quelle, . (Punktbefehl):
Wenn dieser Befehl von der Befehlszeile aus aufgerufen wird, wird ein Skript ausgeführt. In einem Skript lädt ein Quelldateiname die Datei Dateiname. Beim Sourcing einer Datei (Punktbefehl) wird Code in das Skript importiert, der an das Skript angehängt wird (derselbe Effekt wie die # include-Direktive in einem C-Programm). Das Nettoergebnis ist das gleiche, als ob die "Quelltextzeilen" physisch im Hauptteil des Skripts vorhanden wären. Dies ist nützlich, wenn mehrere Skripts eine gemeinsame Datendatei oder Funktionsbibliothek verwenden.
Wenn die Quelldatei selbst ein ausführbares Skript ist, wird sie ausgeführt und gibt die Steuerung an das Skript zurück, das sie aufgerufen hat. Ein ausführbares Skript mit Quellenangabe kann zu diesem Zweck eine Rückgabe verwenden.

Für diejenigen, die mit der Programmiersprache C vertraut sind, wirkt sich das Beschaffen einer Datei ähnlich aus wie die #includeDirektive.

Beachten Sie auch, dass Sie Positionsargumente an die Datei übergeben können, die Sie als Quelle verwenden.

$ source $filename $arg1 arg2 
Wie unterscheidet sich diese Antwort von den 9 vorherigen Antworten? Stephen Rauch vor 6 Jahren 0
Ich füge eine weitere Informationsquelle und zusätzliche Informationen hinzu, die zuvor nicht erwähnt wurden. Alexandro de Oliveira vor 6 Jahren 1
1
w17t

Es sei darauf hingewiesen, dass, obwohl seine ein fantastischer Befehl, weder sourcenoch die Stenografie von .würde Quelle mehr als eine Datei, bedeutung

source *.sh 

oder

. script1.sh script2.sh 

wird nicht funktionieren

Wir können mit forSchleifen zurückgreifen, aber die ausführbare Datei würde oft ausgegeben, mehrere Befehle erstellt oder ausgegeben.

Schlussfolgerung: Es werden sourcenicht mehrere Dateien als Eingabe verwendet. Das Argument muss eins sein.

Was ist IMHO saugt.