Skip to content

Instantly share code, notes, and snippets.

@shengyou
Created August 15, 2016 08:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shengyou/d1d9aa3e0c09b51c1f4e4de9122746bc to your computer and use it in GitHub Desktop.
Save shengyou/d1d9aa3e0c09b51c1f4e4de9122746bc to your computer and use it in GitHub Desktop.
在 bash 裡使用 sudo 將某字串/字段加到檔案的最後面
#!/usr/bin/env bash
# some string 是要加入的字串
# /target/file/path 是要寫入的檔案
# --append 是加到檔案最後,沒加的話就是覆蓋掉整個檔案
# > /dev/null 是防止 tee 輸出
echo 'some string' | sudo tee --append /target/file/path > /dev/null
# 也可以把一段文字加進去
BLOCK="
TEXT...
"
echo "$BLOCK" | sudo tee --append /target/file/path > /dev/null