.Bashrc vs .bash_profile (or just .profile)
First of all both, the ~/.bashrc and the ~/.bash_profile file or like in SUSE Linux just ~/.profile, are configuration files used by the Bash shell to initialize environments for different types of shell sessions.
While both files serve a similar purpose, they are used in different contexts depending on the type of session you are working with.
There are 3 different types of shells in bash: the login shell, normal shell and interactive shell.
In Linux, different types of shell sessions serve different purposes, and the terms login shell, normal shell, and interactive shell describe how the shell behaves and when certain configuration files are executed.
As mentioned under SUSE Linux the ~/.bash_profile is named just ~/.profile as shown below.
On Red Hat Enterprise (RHEL 9) is named ~/.bash_profile as shown below.
Introduction to Bash (Bourne-again Shell)
The Bash shell (Bourne Again SHell) is a command-line interpreter widely used in Unix-like operating systems, including Linux and macOS.
It provides a powerful interface for users to interact with the system, run commands, execute scripts, and automate tasks.
Bash is known for its versatility, supporting features like command history, tab completion, scripting capabilities, and environment customization through configuration files like .bashrc and .bash_profile.
As the default shell in many Linux distributions, Bash is essential for both system administration and everyday tasks.
While Bash is one of the most popular and widely used shells in Linux, several other shells offer unique features and capabilities. Some notable alternatives to Bash include:
- Zsh (Z Shell) -> Zsh is an extended version of Bash with more advanced features and customizations.
- Ksh (KornShell) -> Ksh is one of the oldest Unix shells, offering features from both the original Bourne shell and C shell.
More available shells you will find here https://en.wikipedia.org/wiki/Comparison_of_command_shells
Types of Shells in Linux
- A login shell is a shell session that is created when a user logs into the system. It occurs in scenarios where you need to authenticate or provide credentials to access the system.
- When you log in to a system via a terminal on a physical machine (like on the system console or through a graphical login).
- When you log in via SSH or another remote login method.
- When you switch to another user with su – (the – means “start as a login shell”). More about su – vs su you will find in my following post https://blog.matrixpost.net/whats-the-difference-in-linux-between-the-commands-su-vs-su-without-the-dash/
- A non-login (normal) shell, also referred to as a normal shell, is any shell session that is not associated with a login event. In these sessions, you do not need to authenticate.
- When you open a new terminal window in a desktop environment.
- When you launch a shell session from within an already logged-in system (such as running bash from a terminal).
- An interactive shell is any shell session that allows user interaction (input/output). It can either be a login or a non-login shell, as long as the user can type commands and the shell is expected to respond.
- When you open a terminal and start typing commands.
- When you SSH into a system and type commands.
The ~/.bashrc File
The ~/.bashrc file is executed for non-login (normal) shells or interactive shell sessions, such as when you open a new terminal window or run a terminal emulator.
It is used for setting up command aliases, functions, and other interactive features like the prompt (PS1).
It is typically not used for setting environment variables since it’s executed every time a new interactive shell is opened.
The ~/.bash_profile or ~/.profile File
The ~/.bash_profile or just ~/.profile file is used for login shells. A login shell is as mentioned a session where the user is prompted to log in (e.g., when you first log into a server or system via SSH or a terminal).
It is executed when a user logs in to the system for the first time (interactive login session). Typically, it is used to set environment variables, such as PATH, and to run commands that should only be set once.
Links
How to customize Linux user environments
https://www.redhat.com/sysadmin/customize-user-environmentsUnderstanding shell profiles in RHEL
https://access.redhat.com/solutions/452073