在設定 config 檔案時,第一次可能會用編輯器進行修改,但第二、三次就會用指令的方式進行修改

常常遇到因為權限問題而沒辦法寫入檔案中,沒辦法指令化,範例如下

sudo echo 'foo' > /foo
-bash: /foo: Permission denied

這篇 剛好有人問,因此紀錄一下

tee

tee 指令是從 standard input (stdin) 讀取並輸出 standard output (stdout) 和文件,同時「螢幕輸出」及「輸出檔案」

echo 'foo' | sudo tee /foo

上面的方式會直接覆蓋掉檔案 /foo,如果想要追加寫入檔案的話,可以使用 -a--append 參數 (類似於 >> 的方式)

echo 'foo' | sudo tee -a /foo

如果不想要看到 tee 輸出在螢幕的內容,可以將 tee 的 stdout 丟到黑洞中 /dev/null

echo 'foo' | sudo tee -a /foo > /dev/null