簡體   English   中英

執行和獲取 bash 文件的所有選項有哪些? 為什么不檢查shebang?

[英]What are all options to execute and source a bash file? Why shebang is not checked?

我有一個 bash 文件,我發現我可以對它執行以下操作:

./file.sh               >> executes the file following the chmod -x file.sh rule
bash ./file.sh          >> executes the file 
bash file.sh            >> executes the file 
source ./file.sh        >> source the file code
file                    >> source the file code

我的解釋是正確的還是錯誤的? 我聽說有一些“PATH”相關,但我不知道。

此外,據我所見,shebang/hashbang 並不總是被檢查。 為什么不? 這是我正在使用的示例: #!/bin/echo "This script should be sourced in a shell"

任何見解都會很好; 因為我幾周大到 bash。

你的解釋有點不對勁:

./file.sh               >> executes the file with its requested interpreter
bash ./file.sh          >> executes the file with bash, no matter which language it is
bash file.sh            >> executes the file with bash, no matter which language it is
source ./file.sh        >> sources the code in the current shell
file                    >> executes the file from PATH with its requested interpreter

默認情況下,您應該始終使用./file.sh (如果它在當前目錄中)或file (如果它被重命名並放在 PATH 中的目錄中),因為這樣它可以是 Python 腳本或 Ruby 腳本或 Z021807F8384D1CADE 二進制文件沒關系。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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