5

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

Wuffers
  • 19,619
Manish
  • 239

4 Answers4

5

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.

garyjohn
  • 36,494
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.

1

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.

user10580
  • 281
0

If what you're trying to do is make changing directories easier, look at $CDPATH:

CDPATH The search path for the cd command.  This is a colon-separated list of
       directories in  which  the shell looks for destination directories
       specified by the cd command. A sample value is ".:~:/usr".

Using the example above, add the following to your profile:

CDPATH=.:~:/usr

Then if no matter what directory you are in you can easily get to child directories of ~ or /usr.

$ pwd
/var/log
$ cd bin
$ pwd
/usr/bin
bahamat
  • 5,782