「.myscript」と「./myscript」の違い

「.myscript」と「./myscript」の違い

「1つのドット - スペース - シェルスクリプト名」(例)コマンドと. myshellscript「シェルスクリプトパス」(例)コマンドが./myshellscript混乱します。

彼らは何のためですか?このコマンドは、. myshellscript-rw-rw-r--を使用してもシェルスクリプトを実行することに気づきました。しかし./myshellscriptそれは真実ではない。だから私は混乱しています。

ベストアンサー1

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.

sourceyes の同義語.です。これは両方とも書くことができることを意味します。

. myshellscript

または

source myshellscript

すること:sourceファイルの各行を1行ずつ読み、現在のシェルで実行します。

しかし、./myshellscript 実装する現在のディレクトリのファイル(権限がある場合)これも

/tmp/foo/bar/myshellscript

myshellscript実行ディレクトリのファイル/tmp/foo/bar)または

/usr/local/bin/myshellscript

つまり、ここではポイントは簡単です。現在のディレクトリ。これにより、./myshellscript現在のディレクトリから呼び出されたファイルが実行されます。myshellscript

例えば

cd .

現在のディレクトリに変更します(実際の変更はありません;-))または

ls .

現在のディレクトリの内容を一覧表示します。


@Alvin Wongがコメントしたように、このスクリプトを試すことができます。

#!/bin/foobarNonExisting

echo "This is the Shell that executes me:"
echo $SHELL

または.見てもsourceshebangを読みません。ただ現在のシェルを使用します。スクリプト自体を実行するとエラーが発生します。

おすすめ記事