4

I have alias ready on .bash_aliases.
Problem I'm having right now is that I need to run source ~/.<filename> first before I can execute the alias command.

My question would be how do I run the source ~/.<filename> when user opens terminal.

Radu Rădeanu
  • 169,590
Unknown
  • 195
  • 3
  • 4
  • 9

3 Answers3

5

You might put source ~/.bash_aliases in your ~/.bashrc file, or actually define aliases there.

Some background.

moon.musick
  • 1,892
  • Normally, ~/.bash_alieases file is already sourced in ~/.bashrc file, so you don't need to do it again. See this answer. – Radu Rădeanu Aug 20 '13 at 11:37
  • @RaduRădeanu well I know, but I suppose if the alias doesn't work from .bash_aliases then it might be that the user messed up with sourcing the file. It was my assumption too that if the line is present they may also notice that aliases file is already sourced and ask why it doesn't work, but messed-up syntax or other errors in .bashrc is IMO not relevant to this specific question, at least in its present form. – moon.musick Aug 20 '13 at 11:44
0

Just mention that .bash_alieases script into .profile in your home directory. something like that

cat >> ~/.profile

/bin/sh ~/.bash_alieases

cltd+d

Next time whenever you open a terminal it will execute that .bash_alieases file automatically.

papseddy
  • 506
  • It is not a good idea to source ~/.bash_alieases file in ~/.profile file. This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login exists. Normally, ~/.bash_alieases file is already sourced in ~/.bashrc file. – Radu Rădeanu Aug 20 '13 at 11:36
0

You don't need to source ~/.bash_alieases file in ~/.bashrc file. If you look with attention in ~/.bashrc file you will find somewhere after line 100 the following lines:

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

This mean: if the file ~/.bash_aliases exists and is a regular file, then execute the file. source ~/.bash_aliases and . ~/.bash_aliases are synonymous in bash (see What is the difference between "source" and "." in bash?).

Only if by some mistake you don't have the above lines you should add them again in your ~/.bashrc file.

Finally, if you are interested to run any other file on terminal start up, a good way is to source that file inside ~/.bashrc file as follow:

source ~/<filename>

or, simple:

. ~/<filename>
Radu Rădeanu
  • 169,590