Aliases in .bash_profile do not work

3

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

Manish

Posted 2011-07-27T21:46:46.827

Reputation: 219

sounds like you have a syntax error before your alias definitions. Care to post your .bash_profile here? – glenn jackman – 2011-07-27T21:54:02.260

1what happens when you source .bash_login? – user10580 – 2011-07-27T23:33:33.910

Answers

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

Posted 2011-07-27T21:46:46.827

Reputation: 29 085

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.

Matrix Mole

Posted 2011-07-27T21:46:46.827

Reputation: 3 303

I run 'alias' at the command prompt ....doesnt output anything .But i have everything in there my .bash_profile – Manish – 2011-07-27T22:02:31.383

@Manish, we need to find out where the "command not found" error is coming from. – glenn jackman – 2011-07-27T22:10:39.183

@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 – Manish – 2011-07-27T22:19:31.900

Well I found a temporary hack ....I copied everything into .bashrc and then ran bash . – Manish – 2011-07-27T22:32:18.520

1

You should probably read this section of the bash man page (http://www.gnu.org/software/bash/manual/bashref.html#Bash-Startup-Files) -- that talks about the startup files and which are invoked during the various ways you can start bash.

– glenn jackman – 2011-07-28T02:13:35.840

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

Posted 2011-07-27T21:46:46.827

Reputation: 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

Posted 2011-07-27T21:46:46.827

Reputation: 5 130