看板 Linux 關於我們 聯絡資訊
想請問我寫了一個 shell script 然後用 bash 去執行 跟用 ./ 去執行 在系統上是否有差異呢? 謝謝! -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 101.12.44.16 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Linux/M.1634304222.A.4E3.html
LinBuoRen: 查一下 shebang 跟 Bash 的 POSIX sh 相容模式 10/15 22:00
final01: 基本沒差.. 10/16 13:24
tomsawyer: 不是第一行寫什麼就用什麼執行嗎 10/16 19:09
Bencrie: ./ 這個看第一行 #!/path/to/interpreter 10/16 19:16
scitamehtam: 那用bash 執行的話,script第一行寫別的,這樣優先 10/16 20:41
scitamehtam: 順序不知道怎麼決定 哈哈 10/16 20:41
idisnothing: 你可以試試看 我猜還是bash 10/16 20:43
rickieyang: Fork bash 去執行 script, script 第一行寫別的. 10/16 21:16
rickieyang: bash 會再 fork 第一行寫的往下執行吧 10/16 21:17
Bencrie: 沒有,就是 bash 執行 10/16 21:32
idisnothing: 我剛試了一下 bash會直接忽略第一行的內容 直接往下 10/16 21:36
idisnothing: 執行 tcsh會fork出第一行#!的shell再往下執行 10/16 21:37
idisnothing: https://imgur.com/WEilM8A 10/16 21:41
kdjf: 建議用語法不同的指令測試 (像是for)。我不確定tcsh怎麼處理 10/17 00:13
kdjf: SHELL這個環境變數 10/17 00:13
idisnothing: 被樓上說中了 結果tcsh也是忽略#!那一行直接往下執行 10/17 00:29
idisnothing: 雖然SHELL變數內容為/bin/bash 但實際上是tcsh在執行 10/17 00:30
idisnothing: 指令 https://imgur.com/oYVNe4x 10/17 00:30
idisnothing: 看了一下pstree tcsh確實忽略#!那一行沒錯 10/17 00:43
cole945: 因為#就是註解啊... 10/21 10:02
cole945: 你會說 /* 我覺得gcc忽略我的註解 */ 嗎 10/21 10:02