Re: sudo and echo
On Fri 28 Apr 2023 at 20:35:25 (-0400), Greg Wooledge wrote:
> On Sat, Apr 29, 2023 at 01:52:11AM +0200, coreyh@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.
Cheers,
David.
Reply to: