簡體   English   中英

有什么區別:“。 [script]”或“source [script]”、“bash [script] or $SHELL [script]”、“./ [script]”或“[script]”?

[英]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 . scriptsource script執行的內容script在當前的環境下,即沒有創建一個子shell。 從好的方面來說,這允許script影響當前環境,例如更改環境變量或更改當前工作目錄。 不利的一面是,這允許script影響當前環境,這是一個潛在的安全隱患。

bash script通過scriptbash解釋器來執行。 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM