Creating a 'source' file in the shell uses relative paths

I have a 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 the /opt/monolocation of the myvars file, and not be absolute. How can i do this?

+3
source share
1 answer

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"

They will point to a directory called "subdir" under the directory in which the file is located.

+7
source

Source: https://habr.com/ru/post/1742959/


All Articles