Aliases in .bash_profile do not work

9,829

Solution 1

Aliases are not exported. That is, an alias defined in one shell is not part of the environment inherited by any child shells. Therefore, the best place to define aliases is in your ~/.bashrc, not in your ~/.bash_profile or ~/.profile, as the first will be sourced by any interactive shell while the latter two will be sourced only by login shells.

Solution 2

Use the alias command in the shell to confirm if they are truly getting created or not. Also, are you just using the Mac Terminal.app program? Somewhere in the options should be a setting that you can configure it to use what's called a login shell. That's how the .bash_profile file will be sourced.

Solution 3

The file .bash_profile is probably not being read by your shell on launch. Many distros have something like this in their default .bashrc:

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

If all you're using it for is aliases, I woud recommend you name the file ~/.bash_aliases:

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

All it does it checks if the file exists, and if so, executed the commands in it. In your case, the alias commands. Pop that in your .bashrc and your problems should be solved.

EDIT: Actually it's a little more complicated than that. My solution will work, but this is worth reading .bash_profile vs .bashrc

P.S. A reboot rarely is necessary to fix a problem on a *nix system. A logout and login at most.

Share:
9,829

Related videos on Youtube

Manish
Author by

Manish

Updated on September 18, 2022

Comments

  • Manish
    Manish over 1 year

    I have added some cd blah blah commands as alias in my .bash_login so as to ease changing directories.It was working fine .Suddenly though none of the aliases in there work .It says command not found .I have no clue as to why all of a sudden it would stop working .Any suggestions?(I did reboot my system)

    I apologize I actually meant to write .bash_login

    • Admin
      Admin almost 13 years
      sounds like you have a syntax error before your alias definitions. Care to post your .bash_profile here?
    • Admin
      Admin almost 13 years
      what happens when you source .bash_login?
  • Manish
    Manish almost 13 years
    I run 'alias' at the command prompt ....doesnt output anything .But i have everything in there my .bash_profile
  • Manish
    Manish almost 13 years
    @glenn : how can I do that? let me know .....I probably cannot paste the entire .bash_login here bcoz its huge and contains copyrighted information
  • glenn jackman
    glenn jackman almost 13 years
    You should probably read this section of the bash man page (gnu.org/software/bash/manual/bashref.html#Bash-Startup-File‌​s) -- that talks about the startup files and which are invoked during the various ways you can start bash.