Interview Prep/Shell Scripting/Troubleshooting: tools, portability, permissions and environment

`sudo echo 'vm.swappiness=10' > /etc/sysctl.d/99-tuning.conf` fails with `bash: /etc/sysctl.d/99-tuning.conf: Permission denied`.

BeginnerTroubleshootingTroubleshooting: tools, portability, permissions and environment
Answer

Likely causes → The redirection is performed by your shell, as your user, before sudo runs. Only echo gets root privileges.

Diagnose → The error comes from bash:, not from sudo or echo, which shows the shell opened the file.

Fix → Let a privileged process open the file:

echo 'vm.swappiness=10' | sudo tee /etc/sysctl.d/99-tuning.conf > /dev/null
echo 'extra=1' | sudo tee -a /etc/app.conf > /dev/null        # append
sudo install -m 0644 -o root -g root tuning.conf /etc/sysctl.d/99-tuning.conf

Prevent → Use sudo tee or install in scripts; better still, run the whole provisioning script as root once rather than sprinkling sudo.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Troubleshooting: tools, portability, permissions and environment