How to redirect output to a file and stdout

后端 未结 10 1368
借酒劲吻你
借酒劲吻你 2020-11-22 08:11

In bash, calling foo would display any output from that command on the stdout.

Calling foo > output would redirect any output from that

10条回答
  •  灰色年华
    2020-11-22 08:49

    Bonus answer since this use-case brought me here:

    In the case where you need to do this as some other user

    echo "some output" | sudo -u some_user tee /some/path/some_file
    

    Note that the echo will happen as you and the file write will happen as "some_user" what will NOT work is if you were to run the echo as "some_user" and redirect the output with >> "some_file" because the file redirect will happen as you.

    Hint: tee also supports append with the -a flag, if you need to replace a line in a file as another user you could execute sed as the desired user.

提交回复
热议问题