In the documentation for shell-mode:

Emacs sends the new shell the contents of the file ~/.emacs_shellname as input, if it exists, where shellname is the name of the file that the shell was loaded from. For example, if you use bash, the file sent to it is ~/.emacs_bash. If this file is not found, Emacs tries with ~/.emacs.d/init_shellname.sh.

Everything works as expected when I enter shell-mode with M-x shell. However, this doesn't work when I call term or ansi-term in Emacs. I have both ~/.emacs_bash and ~/.emacs.d/init_shellname.sh containing export PS1='\h:\W$ ', but the prompt is still bash-3.2$.

How to set a startup shell script for term-mode?

1 Answer 1


If you run bash in a term buffer, bash will do all of its normal bash initialisation things, just as it would when you run it inside any other terminal emulator.

So you can set your prompt in your ~/.bashrc file, no differently to the way you do it without Emacs.

If you particularly wish to detect that the shell is running inside Emacs (in order to set some non-standard prompt, for instance), then there's an INSIDE_EMACS environment variable you can test for. e.g.:

if [ -n "$INSIDE_EMACS" ]; then
    # [...]

These days the value of $INSIDE_EMACS varies in useful ways. e.g.:

M-x shell RET

M-x ansi-term RET /bin/bash RET

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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