Stop losing your shell session position after sudo

Environment & Shell Customization (Aliases/Functions/.Bashrc)

Stop losing your shell session position after sudo

🧩 The Challenge

You ever `sudo -i` into root only to realize you are stuck back in the home directory, completely forgetting where you were working in your user session? It happens to me daily and it is infuriating to have to re-path my way through deep subdirectories every time I need elevated privileges.

💡 The Fix

You can make your shell remember the current directory even when you flip to the root account. It is a tiny change to your bashrc that saves me dozens of manual cd commands every single shift.

alias sudo='sudo -E -s'

⚙️ Why It Works

By passing the -E flag, you are preserving your existing environment variables during the transition, and the -s flag ensures you are working in your current context rather than being dumped into root’s home folder.

🚀 Pro-Tip: Use this with a custom bash prompt that clearly displays your UID color so you never accidentally run a delete command in the wrong folder.

Linux Tips & Tricks | © ngelinux.com | 10/6/2026

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Newest
Oldest Most Voted