I want to put new aliases to my
.bash_profile file, but I cannot find this file.
Where is it supposed to be?
I want to put new aliases to my
It's a hidden file, located in your home folder:
~ expands to your home directory. If your username is
user, the result would be:
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.
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.
bash runs as a login shell, in WSL or otherwise1, it runs the first of
.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
.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
(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.)
. 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
.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?
.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 scenarios2:
# 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.
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.)
just wanted to chime in - that even files are hidden in viewer you can still open all of them if you just enter name of the file. For example, if you write in dolphin address bar /home/<user>/.bashrc it will open the file with default editor without toggling show/hide some files.
Other way is to just open your favorite editor and just type in the name of file.
I see that as most preferred way to handle hidden files as showing not showing just add to noise that can be avoided.
And aliases should go into .bashrc as geirha wrote.