I often find myself typing the following commands:

sudo -i
cd workdir

I would like to create a script and name it a single letter. e.g. a so that when I run a the effect is identical to typing the above two commands. In particular: I'm at what in Windows called "elevated" prompt, and I'm in the specified folder.

How do I achieve that?

  • As you are changing process from user bash to root bash with sudo , this cannot be completed. You can create an alias for sudo -i as user than create another alias as root for cd workdir
    – admstg
    Apr 10 at 11:57
  • About the title: you cannot be "staying in sudo, staying in dir" after the script finished. When you run the commands manually, cd workdir and later commands are in a shell run by sudo. This is not your original shell, this is not after sudo; it's "inside" and "during" sudo. Similarly a script that uses sudo (like in this answer) can give you an elevated shell when it's working, not "after the script finished". So the effect can be "identical to typing the above two commands", but it cannot be "after the script finished". Apr 10 at 19:52

1 Answer 1


I guess I've a solution, but it will create multiple processes.

sudo -i bash -c 'cd /path/to/your/workdir && exec bash'

The first process will be created by running the script, the second will be for running bash with sudo -i (I'm not confirm about this), and then exec bash will create another separate process which will give you your desired workdir with elevated privileges.

Except for that last grep bash process, all these processes are created using this single script. If you want to have these much processes, you can use this solution.

➜  ~ ./test.sh
[sudo] password for user1: 
root@mycomputer:/home/user2# ps -aux | grep bash
user1   12875  0.0  0.0   9972  3584 pts/0    S+   18:05   0:00 /bin/bash ./test.sh
root       12876  0.0  0.0  14348  6272 pts/0    S+   18:05   0:00 sudo -i bash -c cd /home/user2 && exec bash
root       12877  0.0  0.0  14348  2380 pts/1    Ss   18:05   0:00 sudo -i bash -c cd /home/user2 && exec bash
root       12878  0.0  0.0  10236  4224 pts/1    S    18:05   0:00 bash
root       12892  0.0  0.0   9080  2560 pts/1    S+   18:05   0:00 grep --color=auto bash

Here, I ran the command as user1 and cd to home dir of user2. Since I use zsh, the first command with arrow is different.


You must log in to answer this question.

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