Piping echo to a file owned by root using sudo and tee

I wanted to create a file with a shell one-liner where the file lived somewhere owned by root.

I tried this but it didn't work:

sudo echo '#!/bin/sh
/usr/bin/tarsnap -c \
  -f "$(uname -n)-$(date +%Y-%m-%d_%H-%M-%S)" \
  /home/simon/team-storage' > /root/tarsnap-backup.sh

Running echo using sudo didn't pass through to the > filename bit.

Here's what did work:

echo '#!/bin/sh
/usr/bin/tarsnap -c \
  -f "$(uname -n)-$(date +%Y-%m-%d_%H-%M-%S)" \
  /home/simon/team-storage' | sudo tee /root/tarsnap-backup.sh > /dev/null

No need for sudo on the echo - but it pipes the output to sudo tee which can then write the file to disk.

The > /dev/null at the end supresses any output from the tee command. If you want to see the output you can do this instead:

echo '#!/bin/sh
/usr/bin/tarsnap -c \
  -f "$(uname -n)-$(date +%Y-%m-%d_%H-%M-%S)" \
  /home/simon/team-storage' | sudo tee /root/tarsnap-backup.sh

Created 2020-08-24T17:30:21-07:00 · Edit