Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

making a shell 'source' file use relative paths

Tags:

shell

I have the file: myvars

MONO_PREFIX=/opt/mono
export MONO_PATH=$MONO_PREFIX/lib/mono/2.0

I "use" it, by calling:

source myvars

I want to change /opt/mono to be relative to the location of the myvars file instead of being absolute. How could I do this?

like image 949
Lucas Meijer Avatar asked Jan 22 '23 04:01

Lucas Meijer


1 Answers

This should work in Bash:

MONO_PREFIX="${BASH_SOURCE[0]%/*}/subdir"

This should work in ksh:

MONO_PREFIX="${.sh.file%/*}/subdir"

And this is for zsh:

MONO_PREFIX="${funcsourcetrace[1]%/*}/subdir"

These will point to a directory called "subdir" below the directory where the file being sourced resides.

like image 193
Dennis Williamson Avatar answered Jan 30 '23 06:01

Dennis Williamson