16

I'm not sure why but my Bash aliases don't seem to work. Here is my .bashrc file

    # v 0.0.1 - 7/03/12

[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*

PATH=$PATH:$HOME/.rvm/bin # Add RVM to PATH for scripting

# expanding history to 10000 commands
export HISTSIZE=10000

# don't store repeated commands more than once
export HISCONTROL=ignoredups

# where to look for Java
export JAVA_HOME=/Library/Java/Home

# tomcat server configuration
export CATALINA_HOME=/usr/local/apache-tomcat-6.0.35

# default editor
export EDITOR=vim

if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi

Here is my .bash_aliases file

# v 0.0.1 - 7/03/12

# aliases for directory traversal
alias ..='cd ../'
alias ...='cd ../../'
alias ....='cd ../../../'

alias gs='git status '
alias ga='git add '
alias gb='git branch '
alias gc='git commit'
alias gd='git diff'
alias go='git checkout '
alias gk='gitk --all&'
alias gx='gitx --all'

alias got='git '
alias get='git '
steve_gallagher
  • 3,778
  • 8
  • 33
  • 51
  • 2
    I like your get/got aliases. As a typo-master, I'm borrowing them. Also, you should not need the trailing space in your aliases. – Mike D Sep 23 '14 at 02:41
  • If you've ssh-ed into a machine, then logout and log back in so that these settings take effect. That fixed it for me. – theQuestionMan Dec 07 '20 at 22:37

10 Answers10

26

Add this to the end of your .bashrc:

if [ -f $HOME/.bash_aliases ]
then
  . $HOME/.bash_aliases
fi
Sean Bright
  • 118,630
  • 17
  • 138
  • 146
  • 11
    If you're going to use "$HOME" instead of a tilde, you should quote it for safety. – Todd A. Jacobs Jul 18 '12 at 19:04
  • 16
    If you are going to be working on a system where $HOME needs to be quoted for safety, you should check into an asylum; you're going to end up there pretty soon anyway. – William Pursell Jul 18 '12 at 19:42
  • @WilliamPursell Still, why ask the shell to do a round of string-splitting when you know that you want something to be parsed as a single argument? – Charles Duffy Jul 18 '12 at 20:35
  • If you're using Bash (`.bashrc`!) use `[[ -f $HOME/.bash_aliases ]]` then you don't have to worry about quoting! – Dennis Williamson Jul 19 '12 at 02:31
  • @Sean Bright I inserted the snippet you suggested, but my alias are still coming up "-bash: ..: command not found". Any ideas? Thanks – steve_gallagher Jul 25 '12 at 14:08
  • 3
    Are you sure your `.bashrc` file is being sourced? Add `set -x` to the beginning of that file. Future shells will print a long list of what it is executing on startup; you'll be able to see if it is processing your `alias` commands. – chepner Jul 25 '12 at 14:36
  • 1
    @Sean Bright, why is `$HOME` needed? Why doesn't `~` work? I'm in the same situation here. – Michael Hoffmann May 01 '17 at 05:56
  • 1
    @MichaelHoffmann https://stackoverflow.com/questions/5930671/why-use-home-over-tilde-in-a-shell-script could be helpful – branweb Mar 27 '18 at 11:39
  • Why is there a dot before $HOME? What does it do? – Aaron Franke Aug 27 '19 at 14:36
  • @AaronFranke The dot is for loading the file equivalent to `source`. This is a common bash pattern "if file exists, load it" – aelmosalamy Jul 19 '23 at 07:11
11

I had a similar problem recently. The solution appeared to be closing ALL open shells (root and user; I didn't notice that I was running a minimized root shell at the time while editing my user .bashrc and .bash_aliases files). The .bash_aliases file then seemed to get read.

user2084149
  • 111
  • 1
  • 2
7

By default

if [ -f ~/.bash_aliases ]; then
.  ~/.bash_aliases
fi

These are available in your .bashrc file in ubuntu 18,19 Actually the problem is sourcing the files, therefore source both files by runing the commands below. I faced the same issues and that is how i solved it.

source ~/.bashrc
source ~/.bash_aliases
Matovu Ronald
  • 782
  • 11
  • 13
2

Bash doesn't look for a file called .bash_aliases; you have to source it explicitly.

Looking around a bit, it appears ~/.bash_aliases is sourced from the default .bashrc on Ubuntu boxes; I don't have access to one to confirm. However, it is not a standard bash configuration file.

chepner
  • 497,756
  • 71
  • 530
  • 681
  • Don't understand the answer. The poster has the lines for .bash_aliases in his .bashrc exactly as you have shown. Why doesn't simply source ~/.bashrc work then? – shawn1874 Nov 19 '22 at 21:03
  • That was added a week after I posted my answer. Note that adding those lines is exactly what the accepted answer suggested doing. The question probably should not have been edited like that in the first place. – chepner Nov 19 '22 at 21:04
  • Interesting. The accepted answer doesn't work for me using bash in KUbuntu. Maybe it's not supported on UBuntu since you mentioned it wasn't a standard config file. I was hoping to find an answer here that worked on UBuntu. – shawn1874 Nov 20 '22 at 22:30
2

I recently installed RVM and changed my terminal profile to "run command as login shell". This disabled .bashrc from loading.

Fix: edit -> profile preferences -> Title and Command -> Run command as a login shell (uncheck)

Find this post for more information, fixed it for me.

https://askubuntu.com/questions/161249/bashrc-not-executed-when-opening-new-terminal

Community
  • 1
  • 1
prodigerati
  • 597
  • 8
  • 26
2

Sometimes forgetting to source the bashrc also creates this problem. So after adding your aliases don't forget to source it.

source ~/.bashrc
Suraj Shrestha
  • 728
  • 11
  • 19
0

You need to include the file. Example code to do so from a default .bashrc file is below:

# Alias definitions.
# You may want to put all your additions into a separate file like
# ~/.bash_aliases, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package.

if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi
Squazic
  • 3,670
  • 3
  • 26
  • 40
0

don't forget to

chmod 600 ~/.bash_aliases

on kubuntu 22.04

;)

0

It may be something simple. Like you are actually running zsh or korn instead of bash. Check your shell. I have done this on installing and testing various flavors. Wasted so much time I now never assume I am on bash anymore.

0

After searching here and there finally I got my solution. As I run my Kali Linux so the default shell is ZSH. You can check yours by using echo $SHELL.

I got my shell /usr/bin/zsh So I just updated by .bash_aliases files to my ~/.zshrc file with this:

if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi

Note: you can change the aliases file or create new one if you don't have any. But remember you must change ~/.bash_aliases' this name which I have chosen according to your preference.

Ked Mardemootoo
  • 1,480
  • 1
  • 5
  • 19