3

I am trying to make a permanent alias (alias homedir='cd /export/home/Files/myName') in unix.

I am trying to add the command in the ~/.bashrc file, but I cannot find the file in my $HOME directory. The only bash file is see is .bash_history, Please help.

I even did a ls -a and still did not find it in my $HOME directory.

Sildoreth
  • 1,883
  • 1
  • 25
  • 38
Nidhin_toms
  • 707
  • 4
  • 18
  • 29

2 Answers2

4

I would advise against placing your aliases directly in ~/.profile (which was suggested in the comments). The ~/.profile file is not specific to bash.

You should instead place your aliases in ~/.bashrc.

Why?

  1. .bashrc is guaranteed to be specific to bash (or at least any future variant of it)
  2. If you set everything up correctly, all of your shells can follow a similar convention. So you can have .bashrc, .zshrc, .tcshrc, .kshrc, etc.

    • If you're like me and enjoy playing around with all the different shells, you will find this very very helpful

How to Set It Up

Place the following code in ~/.profile.

# if running bash
if [ -n "$BASH_VERSION" ]; then
    # include .bashrc if it exists
    if [ -f "$HOME/.bashrc" ]; then
        . "$HOME/.bashrc"
    fi
fi

This will ensure that ~/.bashrc runs if and only if you're using bash. So ultimately, ~/.profile is involved; but you still benefit from having all your aliases (and any other bash-specific commands) in a bash-specific file. The reason you need to add this to .profile is explained here: What's The difference between the different scripts for bash?.

Then add your aliases and other commands to ~/.bashrc. If ~/.bashrc does not already exist, just create it either with touch ~/.bashrc or vi ~/.bashrc.

Remember to re-apply ~/.bashrc whenever you modify it. Otherwise, you won't see the changes. To do that, run this code:

source ~/.bashrc
Community
  • 1
  • 1
Sildoreth
  • 1,883
  • 1
  • 25
  • 38
  • I did place the alias in the .bashrc file. I then relogged into Unix and ran the alias - nothing happened. So that's why I put the Alias in the profile. – Nidhin_toms Mar 11 '15 at 22:41
0

I found the solution. I added the alias to ~/.profile and restarted the session. It worked.

Sildoreth
  • 1,883
  • 1
  • 25
  • 38
Nidhin_toms
  • 707
  • 4
  • 18
  • 29
  • @peterh Actually, this post does answer the original question. It was just worded in such a way that it didn't sound like it. Also, note that the answerer here is the OP. I have updated the wording of the answer to make it clearer. – Sildoreth Mar 12 '15 at 02:37
  • It's ok sildoreth - your answer is more brief - I won't me marking mine as the correct answer even though you could do it that way .. I'll mark yours – Nidhin_toms Mar 12 '15 at 13:12
  • Did you mean you were going to mark my answer as the accepted answer? It looks like you forgot to do this. – Sildoreth Apr 07 '15 at 19:33