sudo cat << EOF > File doesn't work, sudo su does

Output redirection (e.g., >) is performed by bash, not by cat, while running with your UID. To run with root's UID use sudo:

sudo bash -c 'cat << EOF > /etc/yum.repos.d/some-name.repo
line1
line2
line3
EOF'

Another option is tee.

cat << EOF | sudo tee -a /etc/yum.repos.d/some-name.repo
...
EOF

As a variation to @Yuriy Nazarov's answer, only the piped output needs to be elevated thru sudo. The piped input can stay un-elevated:

sudo bash -c 'cat > /etc/yum.repos.d/some-name.repo' << EOF
line1
line2
line3
EOF

This means a much smaller portion of the command needs to be quoted and sent thru to sudo.

Tags:

Shell

Bash

Sudo