Bash – sudo cat << EOF > File doesn’t work, sudo su does

bashshellsudo

I tried the following on the command prompt in bash:

sudo cat << EOF > /etc/yum.repos.d/some-name.repo
#Content
#....
#...
EOF

It complained :

-bash: /etc/yum.repos.d/some-name.repo: Permission denied

Then I did sudo su and tried the exact same thing except the sudo before cat, and it worked without any problem. What am I missing in the above ?

Best Answer

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'