0

I'm not familiar with bash scripting. In my app, I have three folders. Frontend, backend, and script are the three folders. My bash script is contained within the script. This is the structure of my app Image. I'd like to retrieve absolute path variables (frontend and backend) from my script. I'm not sure what to do. I was successful in obtaining the script path folder. My objective is to obtain an absolute route from a script.

#!/bin/sh

root_path="$( cd  "$(dirname "$0")" >/dev/null 2>&1 || exit ; pwd -P )"
#frontend_path=
#backend_path=

echo "Getting absolute path ${root_path}"
6
  • Hi, your shebang read /bin/sh so do you mean bash or posix sh? Commented Jan 26, 2022 at 9:15
  • Sorry, may be it's called shell script. Commented Jan 26, 2022 at 9:17
  • But did you understand my question @kvantour Commented Jan 26, 2022 at 9:18
  • 1
    @Krisna: Please always be precise what kind of shell you are refering to, i.e. whether you are going to use bash, zsh, ksh, POSIX shell or whatever. Your script seems to be intended as POSIX shell script, but we can't know for sure unless you show us how you invoke the script. If you don't make this clear, it could be that you will receive an otherwise valid answer, which for your script language does not make. If you want to be on the safe side, also specify the version of your shell, and the platform. Commented Jan 26, 2022 at 13:19
  • Thank you. I never used script. Could you please help me one thing. Commented Jan 26, 2022 at 15:29

1 Answer 1

1

Adding /.. after the output of dirname should do the trick to get you the root path:

root_path="$( cd  "$(dirname "$0")/.." >/dev/null 2>&1 || exit ; pwd -P )"

From that on, you can just add /frontend / /backend to get the other paths:

frontend_path="$root_path/frontend"
backend_path="$root_path/backend"
Sign up to request clarification or add additional context in comments.

2 Comments

Be adviced that dirname "$0" does not give the absolute path but a relative one.
@kvantour yes, but it doesn't matter, it's pwd that will give the absolute path here ;)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.