bash – ‘source’ は何をするのか?

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

存在していて、実行可能です。なぜUbuntuにはドキュメントがないのでしょうか?それは何をするのですか?それについてのドキュメントをインストールするにはどうすればいいですか?

  641  Andrea Ambu  2009-09-24


ベストアンサー

sourceは、引数として渡されたファイルの内容を現在のシェルで実行するbashシェルの組み込みコマンドです。. (ピリオド) に同義語があります

Syntax

. filename [arguments]

source filename [arguments]

511  None  2009-09-24


気をつけて!./sourceの違い./sourceはちょっと違います

  • ./script はスクリプトを実行ファイルとして実行し、それを実行するために新しいシェルを起動します
  • source scriptは、現在のシェル環境でファイル名からコマンドを読み込んで実行します

注:./script. scriptではなく、. script == source scriptです

Is there a difference between "." and "source" in bash, after all?
I was looking for the difference between the "." and "source" builtin commands and a few sources (e.g., in this discussion, and the bash manpage) suggest that t...

304  damphat  2013-07-11


type」コマンドを知っておくと便利です

> type source
source is a shell builtin

何かがシェルに組み込まれているときは、man bashを実行するときです

94  micans  2013-01-09


.(ピリオド) は、バッシュシェルの組み込みコマンドで、引数として渡されたファイルから現在のシェル内でコマンドを実行します。’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.

40  Jawa  2009-09-24


source’ は ‘.’ コマンドの長いバージョンです。bashプロンプトでは以下のようなことができます

source ~/.bashrc

を使用して、現在実行中の bash の設定 (変更したもの) を再読み込みします

簡略化するとこうなります

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

35  Joakim Elofsson  2009-09-24


sourceコマンドは現在のシェル環境で提供されたスクリプトを実行し(実行許可は必須ではありません)、./コマンドは新しいシェルで提供された実行可能なスクリプトを実行します

sourceコマンドには. filenameと同義語があります

より明確にするために、エイリアスを設定する次のスクリプトを見てください

make_alias

#! /bin/bash

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

これで、このスクリプトを実行するための2つの選択肢ができました。しかし、1つのオプションだけで、この2つのオプションの中から、現在のシェルのための希望のエイリアスを作成することができます

オプション1: ./make_aliasの場合

まずスクリプトを実行可能な状態にします

chmod +x make_alias

Execute

./make_alias

Verify

alias

Output

**nothing**

おっと!新しいシェルでエイリアスがなくなった

2つ目の選択肢で行こう

選択肢2:source make_aliasの場合

Execute

source make_alias

or

. make_alias

Verify

alias

Output

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

“エイリアス “が決まった

29  Harsh Vakharia  2015-03-27


迷ったときは、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.

10  Akshay Upadhyaya  2015-11-11


シェルで “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.

6  Jasser  2015-09-19


Linux Documentation Project, Advanced Bash Scripting Guide, Chapter 15 – 内部コマンドとビルドイン より

ソース、.(ドットコマンド)。 このコマンドは、コマンドラインから起動すると、スクリプトを実行します。スクリプト内では、ソースファイル名はファイル名をロードします。ファイルのソース(dot-command)はスクリプトにコードをインポートし、スクリプトに追加します(Cプログラムの#includeディレクティブと同じ効果)。正味の結果は、「ソース」となるコード行がスクリプト本体に物理的に存在している場合と同じです。これは、複数のスクリプトが共通のデータファイルや関数ライブラリを使用している場合に便利です。 ソースファイル自体が実行可能なスクリプトである場合、そのファイルは実行され、それを呼び出したスクリプトに制御を返します。ソース元の実行可能スクリプトは、この目的のためにリターンを使用することができます

ですから、C言語のプログラミング言語に慣れている人にとっては、ファイルのソースは #include ディレクティブと似たような効果があります

また、ソースとなるファイルに位置引数を渡すことができることにも注意してください

$ source $filename $arg1 arg2

5  Alexandro de Oliveira  2017-06-12


素晴らしいコマンドではありますが、source. の短縮形である source も複数のファイルをソースにすることはできないことに注意してください

source *.sh

or

. script1.sh script2.sh

は動作しません

forのループを使ってフォールバックすることもできますが、実行ファイルを何度も発行したり、複数のコマンドを作成したり、発行したりしてしまいます

結論。sourceは複数のファイルを入力として受け取りません。引数は1つでなければなりません

IMHOは最悪だな

2  None  2017-05-31


ソースを使用すると、別のファイルから変数や関数をスクリプトに渡すことができ、それらを再度書くことなく使用することができます

F.I:

#!/bin/bash

source /etc/environment

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

Cheers

0  DimiDak  2018-11-28


タイトルとURLをコピーしました