Where is .bash_profile? [duplicate]
I want to put new aliases to my .bash_profile file, but I cannot find this file.
Where is it supposed to be?
5 Answers 5
It’s a hidden file, located in your home folder:
(the ~ expands to your home directory. If your username is user , the result would be: /home/user/.bash_profile ).
Since it’s an hidden file, you have to make it visible. To do it in Nautilus go to the «View» menu and check «Show hidden files» (or press the shortcut Ctrl + H ).
If you are using Kubuntu with Dolphin, you have to press Ctrl + . to toggle the visibility of files.
~/.bash_profile is not the right place to put aliases and functions. They should go in ~/.bashrc . See http://mywiki.wooledge.org/DotFiles for an explanation.
yeah, does linux even automatically pull in ~/.bash_profile? I am not seeing it being pulled in automatically when opening a new terminal session
@AlexanderMills, the default when opening a terminal emulator is to run the shell in interactive mode (but not login mode), so .bash_profile is not read since bash only tries to read that in login mode. .bash_profile will be used when you log in via ssh or in a virtual console (ctrl+alt+f1-f6). When you log in graphically, there’s no bash involved, so no .bash_profile is read, however, the graphical login process will run sh and have sh source .profile specifically, before execing the session (e.g. gnome-session). So environment variables set in .profile will be available in the session.
You do not usually have .bash_profile on Ubuntu, nor should you usually create that file. As jpezz says, it would be in your home directory on your Ubuntu system, which is what ~ stands for, and you can create it there. But if you do, you should be careful, because it will prevent bash from automatically running the commands in .profile —which you almost certainly do have.
When bash runs as a login shell, in WSL or otherwise 1 , it runs the first of .bash_profile , .bash_login , or .profile that exists in your home directory. If you have bash -specific commands that you want to run when you log in—but only when bash is your shell—you could put them in .bash_profile . But the mere existence of .bash_profile would prevent .profile from being used.
So then you would want to source .profile from .bash_profile , assuming you wanted those commands to be run too, which you almost always would. You could do that by putting this command in .bash_profile :
(Some readers may be accustomed to seeing that written as . «$HOME/.profile» when it appears in a startup script. That’s always fine—and you may prefer to use that for commands that need to work on shells other than bash too, if you need to accommodate extremely old shells that don’t support standard features, which—well, you don’t need that. In any case, bash always supports tilde expansion and the point of .bash_profile is that only bash runs commands from it, so . ~/.profile is fine.)
The . builtin sources a file, which is to say it runs all the commands from the file in the current shell. Open an interactive bash shell and run help . for more information.
The reason you don’t usually have .bash_profile on Ubuntu even if you have bash -specific commands that you want to run on login is that, by default, users’ .profile files contain code that checks if the current shell is bash , then causes such commands to be run.
There is another answer to the question of where files like .profile or .bash_profile are. The default versions of these files exist in the /etc/skel directory. Files in that directory are copied into the Ubuntu home directories when user accounts are created on an Ubuntu system—including the user account you create as part of installing Ubuntu. If you look in that directory, you’ll notice that there is a file called .profile —as well as some other files like .bashrc —but there is no file called .bash_profile . That is why—or, really, how—no .bash_profile exists in your Ubuntu home directory unless you have created it yourself.
Finally, you should not put aliases in .profile at all, nor is .bash_profile a good place for them, as geirha rightly says. This is because you will want your aliases to work in interactive shells whether or not they are login shells. Instead, define your aliases in .bashrc or, better, .bash_aliases (as guest boeroe points out), which the default .bashrc sources. See How do I create a permanent Bash alias?
The default .profile file will check if you are running a bash shell and source .bashrc if you are:
# if running bash if [ -n "$BASH_VERSION" ]; then # include .bashrc if it exists if [ -f "$HOME/.bashrc" ]; then . "$HOME/.bashrc" fi fi
Meanwhile the default .bashrc will quit at the top without doing anything if it runs in in a non-interactive shell, so commands in it won’t run if it gets sourced by .profile in a non-interactive login shell and in other more obscure scenarios 2 :
# If not running interactively, don't do anything case $- in *i*) ;; *) return;; esac
So if you put aliases in .bashrc or in .bash_aliases , you’ll get them for interactive login shells (where .profile is used) as well as interactive non-login shells (where .bashrc is used). This is what you want. Just don’t put anything above the interactivity check, unless you really, really know what you’re doing.
1 This answer was originally written for a WSL-related question, but that question has been closed as a duplicate and this answer applies fully here as well, so I’ve expanded a bit and posted it here instead.
2 When bash detects that it is probably running as the initial shell of a remote login that is not a login shell—such as when you use ssh to run a single command on a remote machine—it runs commands from .bashrc . (See 6.2 Bash Startup Files in the GNU Bash manual.)
Linux .bash_profile File
The bash shell provides a lot of features that can be configured by using bash configuration files like /etc/profile , ~/.bash_profile , ~/.bash_login etc. The .bash_profile file is located in the current user home directory as a hidden file. In some distributions may use the ~/.bashrc according to the .bash_profile file. In this tutorial, we examine how to find the bash profile file and create it if it doesn’t exist.
Bash Configuration Files
Bash provides different configuration files. The /etc/profile file is used by all users in a Linux system and first, these configurations are applied to the bash shell. And the .bash_profile is located under every user home directory where every user may have different bash configurations. This configuration file overwrites over /etc/profile configuration.
Create .bash_profile File
By default, the .bash_profile is not created by most of the Linux distributions. Actually, the .bash_profile is a normal text file that contains bash configuration like regular bash commands. We can create .bash_profile file like below.
The previous command only creates the .bash_profile file but does not open it to make some changes and configurations. Alternatively following command can be used to create if not exist and edit the .bash_profile file.
Configure .bash_profile File
Bash provides a lot of features even a programming language. All of them can be configured via a bash shell. Also, the .bash_profile file is used to configured newly opened bash shells or terminals. For example, the command prompt is set with the PS1 environment variable and the bash shell command prompt can be changed by setting a new value for the PS1 environment variable in the .bash_profile file.
Open Another User Bash Profile File
The tilda ~ is used to locate the current user home directory but what if we need to open another user bash profile file. We can use the absolute path of the .bash_profile file. In the following example, we open the user ismail bash profile file.
$ vim /home/ismail/.bash_profile
Backup Bash Profile File
Bash profile file changes over time and if there is a big change this may break the profile file. Taking backup of the bash profile file regularly is a good habit. It is very easy to take a backup of the bash profile file. Generally the name .bash_profile.bak is used as the backup file name for bash profile.
$ cp ~/.bash_profile ~/.bash_profile.bak
Systemwide Profile File “/etc/profile”
The /etc/profile file is used as a system-wide profile file where before loading a user-specific profile file this profile file is executed and applied. This system-wide profile file contains checks about shell type, user ID, and loading multiple profile files via /etc/profile.d directory.