Bash startup files decide whether account-level environment and interactive shortcuts load when a user logs in, opens a normal terminal, or starts a noninteractive shell command. Keeping login setup and interactive terminal setup in separate files prevents aliases, functions, and exported variables from appearing in only some sessions.

A Bash login shell reads the first matching file from ~/.bash_profile, ~/.bash_login, and ~/.profile. An interactive non-login Bash shell reads ~/.bashrc, so a common Bash-only pattern is to put exported login values in ~/.bash_profile and have that file source ~/.bashrc only when the login shell is interactive. Plain noninteractive bash -c commands do not read either file by default unless BASH_ENV points them at a startup file.

A user-level PROJECT_HOME value and a workhome alias are enough to test the file split without changing system shell configuration. Creating ~/.bash_profile can stop Bash from reading an existing ~/.profile directly, so keep required login setup in the file Bash will read first.

Steps to configure Bash login scripts:

  1. Back up any existing Bash startup files before editing them.
    $ test -f ~/.bash_profile && cp ~/.bash_profile ~/.bash_profile.bak
    $ test -f ~/.bashrc && cp ~/.bashrc ~/.bashrc.bak

    No output means the file was not present or copied without printing a message.

  2. Add login-shell environment setup to ~/.bash_profile.
    ~/.bash_profile
    # Login Bash shells only
    export PROJECT_HOME="$HOME/projects"
     
    # Load interactive settings when the login shell is interactive.
    case $- in
        *i*)
            if [ -f "$HOME/.bashrc" ]; then
                . "$HOME/.bashrc"
            fi
            ;;
    esac

    If ~/.profile already contains login setup, move the needed lines into ~/.bash_profile or keep using ~/.profile instead. Bash stops after the first readable login startup file.

  3. Add interactive terminal settings to ~/.bashrc.
    ~/.bashrc
    # Interactive Bash shells
    alias workhome='cd "$PROJECT_HOME"'

    Use ~/.bashrc for aliases, prompt settings, completion setup, and functions that only make sense in an interactive terminal.

  4. Check both edited files for Bash syntax errors.
    $ bash -n ~/.bash_profile
    $ bash -n ~/.bashrc

    No output from bash -n means Bash parsed the file without finding a syntax error.

  5. Start a login interactive Bash shell and verify that it loaded both the login export and the sourced alias.
    $ bash --login -ic 'printf "PROJECT_HOME=%s\n" "$PROJECT_HOME"; alias workhome'
    PROJECT_HOME=/home/user/projects
    alias workhome='cd "$PROJECT_HOME"'

    If this command is run from automation without a terminal, Bash can print job-control warnings on stderr. The startup-file result is still shown by the PROJECT_HOME value and alias output.

  6. Start a non-login interactive Bash shell and verify that ~/.bashrc still loads by itself.
    $ bash -ic 'alias workhome'
    alias workhome='cd "$PROJECT_HOME"'

    The alias output proves ~/.bashrc loaded. It does not prove a login-only value such as PROJECT_HOME is present unless the parent session already exported it.

  7. Start a noninteractive Bash command with a clean environment and verify that the startup files stay out of scripted commands by default.
    $ env -i HOME="$HOME" bash -c 'printf "PROJECT_HOME=%s\n" "${PROJECT_HOME-unset}"'
    PROJECT_HOME=unset

    Use BASH_ENV only when a noninteractive Bash script intentionally needs a startup file. Do not point it at an interactive ~/.bashrc that prints prompts, starts terminal helpers, or assumes a keyboard-driven shell.

  8. Open a new terminal or log in again before relying on the new startup files.

    Keep startup files silent unless the shell is interactive. Unconditional output from a login script can interfere with tools that expect machine-readable SSH output.

Share this guide