My Bash aliases don't work
Asked Answered
I

11

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 '
Impassion answered 18/7, 2012 at 18:35 Comment(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.Suburbanize
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.Armelda
R
27

Add this to the end of your .bashrc:

if [ -f $HOME/.bash_aliases ]
then
  . $HOME/.bash_aliases
fi
Rocaille answered 18/7, 2012 at 18:40 Comment(10)
If you're going to use "$HOME" instead of a tilde, you should quote it for safety.Appearance
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.Ardenia
@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?Pyrite
If you're using Bash (.bashrc!) use [[ -f $HOME/.bash_aliases ]] then you don't have to worry about quoting!Irena
@Sean Bright I inserted the snippet you suggested, but my alias are still coming up "-bash: ..: command not found". Any ideas? ThanksImpassion
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.Biltong
@Sean Bright, why is $HOME needed? Why doesn't ~ work? I'm in the same situation here.Mendez
@MichaelHoffmann #5931171 could be helpfulLaritalariviere
Why is there a dot before $HOME? What does it do?Morose
@AaronFranke The dot is for loading the file equivalent to source. This is a common bash pattern "if file exists, load it"Derzon
M
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.

Mentor answered 18/2, 2013 at 17:18 Comment(0)
V
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
Vallo answered 23/4, 2019 at 18:51 Comment(0)
G
3

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

source ~/.bashrc
Gambol answered 31/12, 2018 at 5:25 Comment(0)
B
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.

Biltong answered 18/7, 2012 at 18:41 Comment(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?Meshed
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.Biltong
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.Meshed
P
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

Peddada answered 23/5, 2013 at 2:49 Comment(0)
O
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
Omniumgatherum answered 18/7, 2012 at 18:41 Comment(1)
Same thing, but more concise: [ -f ~/.bash_aliases ] && . ~/.bash_aliasesAppearance
H
0

don't forget to

chmod 600 ~/.bash_aliases

on kubuntu 22.04

;)

Herron answered 17/10, 2022 at 8:43 Comment(0)
S
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.

Sik answered 25/10, 2022 at 19:22 Comment(0)
L
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.

Leighton answered 18/4, 2023 at 18:47 Comment(0)
A
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.

Avigdor answered 16/8 at 19:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.