Home > Blockchain >  How to import files in bash from a different folder
How to import files in bash from a different folder

Time:09-03

my project looks like this:

helper/
      helper_func.sh
main/
      my_program.sh

How do I import helper_func.sh in my_program.sh. If all files are in the same folder I just do source helper_func.sh and I can import the functions but if I do source ../helper/helper_func.sh it can't seem to find it.

What am I doing wrong?

CodePudding user response:

".." is relative to the present working directory, not relative to the script's source code. So it depends what directory you run the script from. Which makes it a bad idea because you generally can't know where someone will run a script from.

One possible trick to use is $0 which generally should contain the location of the script. So...

source `dirname $0`/../helper/helper_func.sh

might do what you want, but you'll have to experiment.

  •  Tags:  
  • bash
  • Related