Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include relative script source file bashrc

Tags:

linux

bash

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

#!/bin/bash
HELPER_DIR=`dirname $0`
.$HELPER_DIR/alias

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
fi

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

like image 636
Vivek Goel Avatar asked Nov 28 '25 04:11

Vivek Goel


2 Answers

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.

like image 157
Igor Chubin Avatar answered Nov 30 '25 16:11

Igor Chubin


You need to leave a space after the "dot"

. $HELPER_DIR/alias
like image 25
pankar Avatar answered Nov 30 '25 17:11

pankar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!