Bash – How to sudo su and change directory just after

bashshellsusudo

I would like to create an alias that does something like this:

alias userYYY='sudo su userYYY; cd /a/path/that/only/userYYY/has/access'

So then from my command line, I am logged in with a sudo user, and I would like to type the alias userYYY so that my shell is now logged with userYYY and pwd is /a/path/that/only/userYYY/has/access.

How can I do that? This userYYY is for running some processes, and there must be anything in its home. Hence, I tried changing its $HOME using:

sudo usermod -m -d /a/path/that/only/userYYY/has/access userYYY 

And then from my shell with my sudoer file I did sudo su userYYY. But that didn't work. The only that worked was sudo su -l userYYYY but that opened a new bash inside my original shell (-bash-4.1$ ....).

In summary, what I want is to simply avoid having to write 2 lines in my shell:

sudo su userYYY
cd /a/path/that/only/userYYY/has/access

Any ideas?

Best Answer

alias userYYY='sudo su userYYY -c "cd /a/path/that/only/userYYY/has/access; /bin/bash"'
Related Question