Skip to main content
edited title
Link
user3450548
  • 3.1k
  • 13
  • 36
  • 56

Shell script - Obtain script current directory (so that I can do include files without relative paths and run the script from everywhere)

Source Link
user3450548
  • 3.1k
  • 13
  • 36
  • 56

Shell script - Obtain script current directory

I have the following problem, my shell script contain something like:

mydir=''

# config load
source $mydir/config.sh

.... execute various commands

My script is placed in my user dir.. let's say /home/bob/script.sh

If I'm inside the /home/bob dir and run ./script.sh everything works fine.

If I'm outside and want to use the absolute path /home/bob/script.sh the config.sh file is not recalled properly.

What value should i assign to $mydir in order to make the script runnable from every path without struggle?

mydir=$(which command?)

PS: as bonus please also provide an alternative if the script dir is inside the $PATH