Linux – Try to create a permanent alias – UNIX

Try to create a permanent alias – UNIX… here is a solution to the problem.

Try to create a permanent alias – UNIX

I’m trying to create a permanent alias in Unix (alias homedir='cd/export/home/Files/myName').

I

tried to add a command to the ~/.bashrc file, but I can’t find the file in my $HOME directory. The only bash file you see is .bash_history, please help.

I even executed ls -a and still haven’t found it in my $HOME directory.

Solution

I recommend not putting your alias directly in ~/.profile (suggested in the comments). The ~/.profile file is not specific to bash.

You should put your alias in ~/.bashrc.

Why?

  1. The .bashrc guarantee is specific to bash (or at least any future variant of it).
  2. If you set everything up correctly, all your shells can follow similar conventions. Therefore, you can have .bashrc, . zshrc、. tcshrc、. KSHRC and others.

    • If you’re like me and enjoy playing with all kinds of different shells, you’ll find this very, helpful

How to set it up

Put 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 use bash.
So eventually, ~/.profile gets involved; But you still benefit from putting all aliases (and any other bash-specific commands) in a bash-specific file. The reason you need to add it to the .profile is explained here: What’s The difference between the different scripts for bash? .

Then add your alias and other commands to ~/.bashrc. If ~/.bashrc doesn’t exist, just create it with touch ~/.bashrc or vi ~/.bashrc.

Remember to reapply ~/.bashrc whenever you modify it. Otherwise, you won’t see the changes. To do this, run this code:

source ~/.bashrc

Related Problems and Solutions