我最近看到以下行:
curl -fsSL https://deb.nodesource.com/setup_17.x | bash -
bash和 和有什么不一樣bash -?我嘗試運行這兩種變體,它們似乎做同樣的事情。
man bash也沒有提供答案。
uj5u.com熱心網友回復:
命令是相同的。
作者可能認為-作為引數傳遞將使 bash 從標準輸入讀取命令,但 bash 無論如何都會這樣做。
事實上,bash 手冊頁解釋了這-等同于--并終止選項處理:
-- A -- signals the end of options and disables further option
processing. Any arguments after the -- are treated as file‐
names and arguments. An argument of - is equivalent to --.
請注意,如果 之后有某些內容-,則行為可能會有所不同。例如:
$ echo "echo foo" > ./-c
$ chmod x ./-c
$ export PATH=.:"$PATH"
$ bash -c "echo bar"
bar
$ bash - -c "echo bar"
foo
$
另請注意, using并非bash -將其用作登錄 shell。這可以通過添加一些可列印的東西來證明~/.bash_profile:
$ export HOME=$(mktemp -d)
$ cd
$ echo "echo login shell" > .bash_profile
$ echo "echo hello" | bash
hello
$ echo "echo hello" | bash -
hello
$ echo "echo hello" | bash -l
login shell
hello
$
“登錄shell是引數零的第一個字符是-”的聯機幫助頁中的含義是命令名稱以連字符開頭:
$ cat .bash_profile
echo login shell
$ ln -s /bin/bash ./notLoginShell
$ ln -s /bin/bash ./-isLoginShell
$ export PATH=~:"$PATH"
$ echo "echo hello" | notLoginShell
hello
$ echo "echo hello" | -isLoginShell
login shell
hello
$
附錄
使用bash -可能是歷史上用于防止特定型別安全漏洞的技術的貨物崇拜結轉。
POSIX評論:
在支持 set-user-ID 腳本的系統上,歷史上的陷門將腳本鏈接到 name
-i。當它被一個序列呼叫時,例如:sh -或通過:
#! usr/bin/sh -歷史系統假定后面沒有選項字母。因此,這卷 POSIX.1-2017 允許單個 <hyphen-minus> 標記選項的結束,除了使用常規的“
--”引數,因為它被認為是舊的做法是如此普遍。
unix-faq解釋:
假設腳本被呼叫
/etc/setuid_script,開始于:#!/bin/sh現在讓我們看看如果我們發出以下命令會發生什么:
$ cd /tmp $ ln /etc/setuid_script -i $ PATH=. $ -i我們知道最后一個命令將被重新排列為:
/bin/sh -i但是這個命令會給我們一個互動式的shell,腳本所有者的setuid!
幸運的是,這個安全漏洞可以通過第一行輕松關閉:
#!/bin/sh -選項串列結束的
-信號:下一個引數-i將被視為要從中讀取命令的檔案的名稱,就像它應該的那樣!
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/512822.html
標籤:重击壳
下一篇:如何從csv中獲取多個單詞
