[英]What's the difference between: “. [script]” or “source [script]”, “bash [script] or $SHELL [script]”, and “./ [script]” or “[script]”?
我知道那個source
和.
做同樣的事情,如果標題中的其他命令對不是同樣的事情,我會很驚訝(因為我正在運行 bash 作為我的 shell, $SHELL [script]
和bash [script]
是等價的,對吧??)。
那么三種執行腳本的方法有什么區別呢? 我問是因為我剛剛了解到獲取腳本與執行它並不完全相同。 在某種程度上,我沒有發現運行我的“實驗”和閱讀手冊頁很明顯。
通過在我編寫的極其簡單的腳本上盲目調用這些函數,我無法發現其他哪些細微差別? 閱讀上面鏈接的答案后,我可以強烈猜測我的問題的答案將是一個非常簡單的解釋,但以我自己幾乎從未完全發現的方式。
這是我所做的“實驗”:
$. myScript.sh
"This is the output to my script. I'd like to think it's original."
$source myScript.sh
"This is the output to my script. I'd like to think it's original."
$bash myScript.sh
"This is the output to my script. I'd like to think it's original."
$$SHELL myScript.sh
"This is the output to my script. I'd like to think it's original."
$./myScript.sh
"This is the output to my script. I'd like to think it's original."
$myScript.sh
"This is the output to my script. I'd like to think it's original."
. script
. script
和source script
執行的內容script
在當前的環境下,即沒有創建一個子shell。 從好的方面來說,這允許script
影響當前環境,例如更改環境變量或更改當前工作目錄。 不利的一面是,這允許script
影響當前環境,這是一個潛在的安全隱患。
bash script
通過script
到bash
解釋器來執行。 script
本身給出的任何shebang都會被忽略。 (“Shebang”指的是script
的第一行,例如可以讀取#!/bin/bash
或#!/usr/bin/perl
或#!/usr/bin/awk
,以指定要使用的解釋器.)
$SHELL script
通過script
來無論是當前的shell解釋器來執行。 那可能是也可能不是bash
。 (環境變量SHELL
保存您當前的 shell 解釋器的名稱。如果運行 bash, $SHELL
被評估為/bin/bash
,其效果在上一段中詳述。)
./script
執行當前工作目錄中文件script
的內容。 如果沒有這樣的文件,就會產生一個錯誤。 $PATH
的內容對發生的事情沒有影響。
script
在$PATH
列出的目錄中查找文件script
,該目錄可能包含也可能不包含當前工作目錄。 執行此目錄列表中的第一個script
,它可能是也可能不是您當前工作目錄中的script
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.