Take the 2-minute tour ×

I have multiple bash file. I want to write a master bash file which will include all required bash file in current directory. I tried like this

HELPER_DIR=`dirname $0`

But I when I put following line in my $HOME/.bashrc

if [ -f /home/vivek/Helpers/bash/main.bash ]; then
    . /home/vivek/Helpers/bash/main.bash

I am getting error no such file ./alias. File alias is there. How can I include relative bash file ?

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

Use $( dirname "${BASH_SOURCE[0]}" ) instead.

I added these two lines two my ~/.bashrc:

echo '$0=' $0
echo '$BASH_SOURCE[0]=' ${BASH_SOURCE[0]}

and started bash:

$ bash
$0= bash
$BASH_SOURCE[0]= /home/igor/.bashrc

There is a difference between $0 and $BASH_SOURCE when you start a script with source (or .) or in ~/.bashrc.

share|improve this answer
add comment

You need to leave a space after the "dot"

. $HELPER_DIR/alias
share|improve this answer
add comment

Your Answer


