Keep aliases and the prompt in ~/.bashrc.
Make an interactive login Bash source that file when it exists.
Which line is appropriate in ~/.bash_profile?
Check that ~/.bashrc exists and source it in the current shell so aliases and variables are retained.
Detailed explanation
exec $HOME/.bashrcIncorrect. exec would replace the current shell instead of sourcing the settings.
Incorrect. exec would replace the current shell instead of sourcing the settings.
[ -f "$HOME/.bashrc" ] && . "$HOME/.bashrc"Correct. It checks for the file and sources it into the login shell.
Correct. It checks for the file and sources it into the login shell.
bash $HOME/.bashrcIncorrect. Running bash creates a child shell, so changes do not persist in the parent login shell.
Incorrect. Running bash creates a child shell, so changes do not persist in the parent login shell.
export $HOME/.bashrcIncorrect. export does not read a configuration file.
Incorrect. export does not read a configuration file.
Try it yourself
An example you can run in a temporary verification environment.
bash --noprofile --norc -c 'work_dir=$(mktemp -d); profile="$work_dir/.bashrc"; printf "SHARED_SETTING=loaded\n" > "$profile"; [ -f "$profile" ] && . "$profile"; printf "%s\n" "$SHARED_SETTING"; rm -f -- "$profile"; rmdir -- "$work_dir"'Expected result
loadedKey points
- Existence check
- Source in current shell
- No child shell
Notes
- Environment: GNU Bash 5.2 / 一時ホーム
- Command output formatting can vary slightly by distribution or tool version.
- Run the example in a temporary directory or process when possible.
Foundation review
Read the scope first
Check whether the command acts on the current shell, a new process, an existing process, or a file.
Verify the observable result
Use the supplied command and compare the output with the expected result.