17

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 '
2
  • 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
    Commented Sep 23, 2014 at 2: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. Commented Dec 7, 2020 at 22:37

11 Answers 11

27

Add this to the end of your .bashrc:

if [ -f $HOME/.bash_aliases ]
then
  . $HOME/.bash_aliases
fi
10
  • 11
    If you're going to use "$HOME" instead of a tilde, you should quote it for safety. Commented Jul 18, 2012 at 19:04
  • 17
    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. Commented Jul 18, 2012 at 19:42
  • 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
    Commented Jul 25, 2012 at 14:36
  • 1
    @Sean Bright, why is $HOME needed? Why doesn't ~ work? I'm in the same situation here. Commented May 1, 2017 at 5:56
  • 1
    @MichaelHoffmann stackoverflow.com/questions/5930671/… could be helpful
    – branweb
    Commented Mar 27, 2018 at 11:39
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.

0
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
3

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

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

3
  • 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
    Commented Nov 19, 2022 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
    Commented Nov 19, 2022 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
    Commented Nov 20, 2022 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

0
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
1
  • Same thing, but more concise: [ -f ~/.bash_aliases ] && . ~/.bash_aliases Commented Jul 18, 2012 at 19:05
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.

0

Don't be like me, friends. I was pulling my hair out trying to figure out why my aliases wouldn't work. All I had to do was close and reopen the terminal for it to take effect.

1
  • Your answer may be helpful to some people, but try to stick to the point and avoid personal anecdotes. Commented Aug 22 at 1:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.