64

I want to put new aliases to my .bash_profile file, but I cannot find this file.
Where is it supposed to be?

0

5 Answers 5

59

It's a hidden file, located in your home folder:

~/.bash_profile

(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.

3
  • 3
    The Keys Ctrl-H toggles the show/hide hidden files also.
    – Ice
    Mar 6, 2011 at 17:15
  • sweet didn't know about that ctrl+H Mar 6, 2011 at 23:58
  • ls -al shows hidden files if ctrl -h doesnt work Sep 22, 2016 at 14:01
16

~/.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.

2
  • yeah, does linux even automatically pull in ~/.bash_profile? I am not seeing it being pulled in automatically when opening a new terminal session Dec 9, 2016 at 21:47
  • @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.
    – geirha
    Dec 10, 2016 at 12:45
10

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 otherwise1, 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:

. ~/.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 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.

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.)

1
  • Nice and comprehensive answer! So in a nutshell, is it okay to create ~/.bash_profile, e.g., to start a Docker daemon and just append . ~/.profiles or should it be avoided?
    – F1iX
    Apr 28, 2022 at 10:54
5

It is handy to put all your alias in ~/.bash_aliases in the user home directory. That is what is suggested in the ~/.bashrc file in the comments.

0

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.

Not the answer you're looking for? Browse other questions tagged .