I logged in using my username ravbholua:

ravbholua@ravbholua-Aspire-5315:~$ echo $LOGNAME ravbholua

I create file named a1:

ravbholua@ravbholua-Aspire-5315:~$ echo>a1
ravbholua@ravbholua-Aspire-5315:~$ ll a1
-rw-rw-r-- 1 ravbholua ravbholua 1 Oct  8 09:57 a1

As expected the above file has me (ravbholua) as owner.

Next I create a2 using sudo with echo command:

ravbholua@ravbholua-Aspire-5315:~$ sudo echo>a2
ravbholua@ravbholua-Aspire-5315:~$ ll a2
-rw-rw-r-- 1 ravbholua ravbholua 1 Oct  8 09:57 a2

The owner is me only, i.e. ravbholua.

Now I create a3 using sudo again but with vim command:

ravbholua@ravbholua-Aspire-5315:~$ sudo vim a3
ravbholua@ravbholua-Aspire-5315:~$ ll a3
-rw-r--r-- 1 root root 10 Oct  8 09:57 a3

Oh! how come the owner changes now. It's not me but root. Why such variation with echo and vim! It's a surprise that with change of commands how can the owner of the created file changes.

2 Answers 2


The second example runs echo under sudo, but the redirection happens under the original shell.

sudo bash -c "echo > a4"
  • Thanks! Just one confirmation w.r.t. the example sudo echo>a2: the original shell 1st creates the file and then sudo is run. So when the shell creates file, it makes the one owner of the file who is responsible for running the complete command sudo echo>a2
    – Ravi
    Oct 8, 2013 at 8:21
  • The order it happens in should be considered indeterminate. The result is the same though. Oct 8, 2013 at 8:28
  • The redirection happens before the shell knows that it is running sudo, so the owner is the same as if you had just run echo > a2. You can get an error if the sudo user does not have write permission for a2 (I run into this with an NFS mount that is read-only for root.)
    – chepner
    Oct 8, 2013 at 13:39
  • @chepner Great! This is what I wanted - a confirmation and you did it perfectly. Many thanks to you.
    – Ravi
    Oct 9, 2013 at 3:17

sudo is just a command.

So for your first example: sudo echo>a2

sudo is the command. And while you have the redirection butted up against the echo, that doesn't mean anything.

For example: echo cat /etc/passwd > /tmp/foo You won't get the contents of /etc/passwd in /tmp/foo. You'll just have cat /etc/passwd. sudo is no different. The echo part is just an argument to sudo. sudo does use it to execute things, but the shell doesn't know that.

That's one of the ways where sudo access is very different from doing things as root.

For your example where you use sudo vim a3 sudo runs vim a3 for you as root. And vim runs as root, so the file is owned by root.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .