0

I have a script which sets some variable, now I know to set them for a session I should source it. But my problem is I am using some relative path in the script, so if I source the script those path don't work. How to solve this. Please see my example script:

#!/bin/bash

BASEDIR=$(dirname "$0")
source $BASEDIR/../.text_formatter #Invoke the formatter bash script

export var1 = 1

I have added the path of the script in PATH, so that I can run this script as a program. But if I source the script I get error no such file or directory: ./../.text_formatter. If I dont source it, obviously the var1 value does not persist. How to solve this issue ? I would like to avoid source, so that user can directly run this as a program, but would like to know if solution exists with source also.

0

1 Answer 1

1

You can't work around the need to source it since otherwise the script will run in a subprocess and cannot affect the shell.

However, you can fix the relative path issue using the solution outlined here.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.