Skip to main content
Post Closed as "Duplicate" by muru, Michael Homer, Jeff Schaller
added 130 characters in body
Source Link
MrDuk
  • 1.7k
  • 2
  • 14
  • 28

I have a script that I call via . ./env/setenv.sh. In this script, I'm calling a python script that's also located in ./env/.

My current directory is along the lines of ~/dev/project/ and I'm calling into ~/dev/project/env via . ./env/setenv.sh

My attempts (${dirname $0}) to reference the location of setenv.sh from within itself hasn't been successful, because my current working directory is considered /bin rather than ~/dev/project/env/ (I assume due to me sourcing it with .)

What other options do I have for referencing the current working directory of the setenv.sh script, so that it can call my .py script from within the same directory?

e.g.,

WORKING_DIR=$(dirname "$0")
echo $WORKING_DIR
export VAR=$(python $WORKING_DIR/script.py)"

When called via . ./env/setenv.sh prints: /bin, and can't locate script.py

I have a script that I call via . ./env/setenv.sh. In this script, I'm calling a python script that's also located in ./env/.

My attempts (${dirname $0}) to reference the location of setenv.sh from within itself hasn't been successful, because my current working directory is considered /bin (I assume due to me sourcing it with .)

What other options do I have for referencing the current working directory of the setenv.sh script, so that it can call my .py script from within the same directory?

e.g.,

WORKING_DIR=$(dirname "$0")
echo $WORKING_DIR

When called via . ./env/setenv.sh prints: /bin

I have a script that I call via . ./env/setenv.sh. In this script, I'm calling a python script that's also located in ./env/.

My current directory is along the lines of ~/dev/project/ and I'm calling into ~/dev/project/env via . ./env/setenv.sh

My attempts (${dirname $0}) to reference the location of setenv.sh from within itself hasn't been successful, because my current working directory is considered /bin rather than ~/dev/project/env/ (I assume due to me sourcing it with .)

What other options do I have for referencing the current working directory of the setenv.sh script, so that it can call my .py script from within the same directory?

e.g.,

WORKING_DIR=$(dirname "$0")
echo $WORKING_DIR
export VAR=$(python $WORKING_DIR/script.py)"

When called via . ./env/setenv.sh prints: /bin, and can't locate script.py

Source Link
MrDuk
  • 1.7k
  • 2
  • 14
  • 28

How can I reference the current directory of a script when sourcing it?

I have a script that I call via . ./env/setenv.sh. In this script, I'm calling a python script that's also located in ./env/.

My attempts (${dirname $0}) to reference the location of setenv.sh from within itself hasn't been successful, because my current working directory is considered /bin (I assume due to me sourcing it with .)

What other options do I have for referencing the current working directory of the setenv.sh script, so that it can call my .py script from within the same directory?

e.g.,

WORKING_DIR=$(dirname "$0")
echo $WORKING_DIR

When called via . ./env/setenv.sh prints: /bin