sudo でリダイレクトするには

sudo の実行結果をリダイレクトしようとするとエラーになります。

$ sudo echo "TEST" > /root/test.txt
-bash: /root/test.txt: Permission denied

このような場合は sudo sh -c の引数として一連のコマンドを実行します。

$ sudo /bin/sh -c 'echo "TEST" > /root/test.txt'

参考までに、「-c」オプションは man によると以下のように書かれています。

-c Read commands from the command_string operand instead of from the standard input.
Special parameter 0 will be set from the command_name operand and the positional
parameters ($1, $2, etc.) set from the remaining argument operands.

コメント

タイトルとURLをコピーしました