In this example I will use the sources.list file that contains a list of the repositories from which we can get packages.
Firstly:
results in a "Permission denied error". This is because the >> /etc/apt/sources.list redirection is processed by your shell before spawning sudo, so it is trying to open the file with your (ordinary) user privileges, not root privileges.
Ways around this:
1)
The shell spawned by sudo reads its command from the string after the -c switch, so the redirection is done in a process which is already running as root.
2)
The 'tee' command reads from standard input and the -a switch appends it to the file. Again, the file is opened by the process spawned by sudo, which is running as root.
3)
An alternative formulation of 2), using a HereString.
No page links to IORedirectionNotes.