Home > Mobile >  How to change to script directory in shell?
How to change to script directory in shell?

Time:04-14

I am trying to run a python script that resides in the directory of my shell script, but I cannot find out how to dynamically change directory in the shell script, so I could execute it reliably.

Most solutions I've seen are using features available in bash, but I can only use shell since I am using alpine and do not want to install bash. The shell script is something like this:

export FLASK_APP="./app.py"
export FLASK_RUN_HOST='0.0.0.0'
export FLASK_RUN_PORT=5000
flask run

And app.py is in the same directory of my script.

CodePudding user response:

You seem to be running Alpine Linux which uses the ash shell by default:

Note: By default Alpine Linux uses the ash shell *

So try this, adapted to ash from the one-liner in this answer:

my_dir="$( cd -- "$(dirname -- "$0")" && pwd )"
echo "I'm running in $my_dir"

It works for me in Ubuntu's dash, which is a derivative of ash.

  • Related