On Fri, Apr 28, 2023 at 10:10:03PM -0500, David Wright wrote: > On Fri 28 Apr 2023 at 20:35:25 (-0400), Greg Wooledge wrote: > > On Sat, Apr 29, 2023 at 01:52:11AM +0200, cor...@free.fr wrote: > > > $ sudo echo 123 > /root/123.txt > > > > > > It tells me "permission rejected". > > > > > > Why this sudo can't get success? > > > > Because the redirection is done by your shell before sudo is executed. > > > > See <https://mywiki.wooledge.org/BashPitfalls#pf53> for suggestions, > > but basically you're looking at variants of: > > > > sudo sh -c 'echo 123 > /root/123.txt' > > You could also use the construction: > > $ echo 123 | sudo tee /root/123.txt > /dev/null > > which has the advantage, when required, that the program producing > the output doesn't have to run with root privileges.
My favourite alternative to tee for this is dd: you don't have to "throw away" the other stream: echo 123 | sudo dd of=/root/123.txt Actually, any program willing to pass stdin to an output file whose name you can choose will do the trick. Cheers -- t
signature.asc
Description: PGP signature