How to make aliases automatically work at startup?

3.3k views Asked by At

I have my aliases in ~/.bash_aliases

I used to run ubuntu and these aliases automatically worked from startup.

Now when I switched to windows and started to use Git Bash, I have to use

source ~/.bash_aliases

or

. ~/.bash_aliases

to get them to be recognized by the bash

How can I get them to work without using this command every time I open bash?

I think it's something i have to do in the ~/.bashrc

3

There are 3 answers

0
1218985 On

Yeah, you're right! To configure bash aliases, you can put them in .bashrc file in your home directory. For instance:

cd
echo alias ll=\'ls -ltrh\' >> .bashrc

Next, you need to either source this file (i.e. run source .bashrc) or restart to your terminal to take this changes into effect.

If you have an aliases.sh file (an equivalent for .bashrc file) available in C:\Users\<username>\AppData\Local\GitHub\PortableGit_\etc\profile.d\aliases.sh or under C:\Program Files\Git\etc\profile.d\aliases.sh, you can make use of that file too.

0
G_G On

Your .bashrc gets sourced by bash as you login or open a new bash shell. Anything you source within your .bashrc - will also get sourced.

So in your case placing

[ -f ~/.bash_aliases ] && . ~/.bash_aliases

within your .bashrc , should do the trick.

0
julian On

There are various files which get loaded if they exists on startup depending on how it is invoked.

Invoked as a login shell

When Bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable. The --noprofile option may be used when the shell is started to inhibit this behavior.

When an interactive login shell exits, or a non-interactive login shell executes the exit builtin command, Bash reads and executes commands from the file ~/.bash_logout, if it exists.

Invoked as an interactive non-login shell

When an interactive shell that is not a login shell is started, Bash reads and executes commands from ~/.bashrc, if that file exists. This may be inhibited by using the --norc option. The --rcfile file option will force Bash to read and execute commands from file instead of ~/.bashrc.

So, typically, your ~/.bash_profile contains the line

if [ -f ~/.bashrc ]; then . ~/.bashrc; fi after (or before) any login-specific initializations.

You can source your bash_aliases file from within your .bashrc file by adding

[ -f ~/.bash_aliases ] && . ~/.bash_aliases

Also this is helpful post to understand the difference between .bashrc and .bash_profile