I was following the instructions in https://stackoverflow.com/questions/2135644/how-can-i-define-a-bash-alias-as-a-sequence-of-multiple-commands to create my own bash script. It is as follows:

sudo -u otheruser -i
alias cd1="cd /dir/one/"
alias cd2="cd /dir/two/"

I want these aliases to be available to otheruser. My goal is to run it as an executable but for testing I am sourcing it (to avoid the problem in https://stackoverflow.com/questions/2197461/how-to-set-an-alias-inside-a-bash-shell-script-so-that-is-it-visible-from-the-ou) like source ./myscript.

Even with doing that, the sudo works, but none of the following aliases are available (for otheruser). Do you think the aliases are being applied to the user that invoked this command, and not to the new user? Or is there some rule that commands in a script are ignored after sudo'ing to another user?

  • Add this to /etc/profile (can depends of your distro) Apr 2, 2018 at 16:44
  • Or ~otheruser/.profile Apr 2, 2018 at 16:45
  • I am not sure that either of those ideas will work. I am intentionally not adding it to ~otheruser/.profile because I don't want it to take effect whenever that other user logs in (I don't want to interfere with that user). I only want to enable these commands when I sudo as that user, by running my script. // As for adding this to /etc/profile if by that you mean my own login script, I don't see how that will help because I want the commands to be available when I'm sudo'd as the other user.
    – Stephen
    Apr 2, 2018 at 16:59
  • If you need just to alias cd command you can use the autojump command just j one to cd /dir/one see here : autojump on github
    – GAD3R
    Apr 2, 2018 at 17:55

1 Answer 1


I think that the reason it doesn't work is that the two alias commands are run after bash session started by sudo finishes. You could make a test - source the script, when the new session starts press Ctrl-d to leave it and then in the original shell type:

$ type cd1
cd1 is aliased to `cd /dir/one/'
$ type cd2
cd2 is aliased to `cd /dir/two/'

I can achieve what you want with expect script but I'm not an expert on that so criticism is welcome:

#!/usr/bin/expect -f

spawn sudo -u otheruser -i
send "alias cd1='cd /dir/one/'"
send "\n"
send "alias cd2='cd /dir/two/'"
send "\n"


Make sure you actually have expect installed before running this script.

You might be able to install expect and tcl (which it requires) using your system's package manager. If you need to install it manually, see the instructions for Expect at http://expect.sourceforge.net/. The README/INSTALL files for Expect point you at some locations for getting Tcl, but these seem out of date. However you can get a copy of Tcl from ActiveState.

  • Thanks for the suggestion. I don't have expect. I downloaded it and was going to install it, but it also requires tcl and I don't want to install that as well so I'm going to skip it for now.
    – Stephen
    Apr 2, 2018 at 17:45
  • Why not? It doesn't take much space. It won't break anything, too. BTW, what distro do you use? Apr 2, 2018 at 17:45
  • It's just a lot of time expenditure for this task since it was just a little annoyance I wanted to solve, not a core issue for a project. I'm using Red Hat at work but we don't have permission to install new packages. I did extract expect to a folder in my user directory and extracted ActiveState's Tcl Linux distro to another folder (so they are side-by-side like it requested) but it wasn't able to find it.
    – Stephen
    Apr 2, 2018 at 17:58
  • -bash-4.1$ ./configure checking for correct TEA configuration... ok (TEA 3.9) configure: configuring expect 5.45.4 checking for Tcl configuration... configure: error: Can't find Tcl configuration definitions
    – Stephen
    Apr 2, 2018 at 17:58
  • 1
    Do you have a working expect binary? If you do remember you have to change shebang line from /usr/bin/expect to point to the expect you just compiled or run the script expect script. Apr 2, 2018 at 18:18

You must log in to answer this question.

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