I am working on a machine that has some user accounts that are dynamically generated, and therefore I can not user customize the .bashrc or .profile.

I frequently need to use these accounts to change some configurations; to do so I sudo su into them.

My issue is that on my user account I have configured a lot of aliases that I have come to use without even thinking about, aliases that do not exist for this sud user.

Is there a way, from my shell, to automatically source a specific file when only I su to a user?


you can simply start your bash with the --rcfile /path/to/script flag. to be specific, use

sudo -u $USER bash --rcfile /path/to/file

make sure not to cover bash --rcfile /path/to/file by double quotes, because if so, sudo will not find the bash command.

You could also make a function containing that with the user as parameter $1

  • He there, I just tried this out but it does not seem to be working. I tried it with the given command however I do not have the pass of the user I am trying to become, I am using sudo. I tried prepending sudo to this command with no luck. bash: cannot set terminal process group (-1): Inappropriate ioctl for device bash: no job control in this shell – Matt Clark Aug 10 '15 at 14:55
  • 1
    Oh... no sorry, didn't read your question well. if you use sudo you better should use sudo -u $USER bash --rcfile /path/to/file. in that case you could function that like sudo -u $1 bash --rcfile /path/to/file – Schwertspize Aug 10 '15 at 16:09
  • Awesome. You click edit to edit your answer, this is an acceptable solution. – Matt Clark Aug 10 '15 at 17:31
  • yes, I did it, but before you texted that, I was editing another answer because the question was edited.... – Schwertspize Aug 10 '15 at 17:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.