Dumb question on script execution

Hi Folks -

I have a dumb question.

Why does this work:

pushd "/apps/scripts"
./script.sh
popd

But this doesn't:

./apps/scripts/script.sh

Is it that obvious where I'm overlooking it?

Then "<path to script>" != ./apps/scripts/ , I'd say. Check with e.g. ls .

That's the thing - it's the same path.

What does not work? What does the script do? A difference is that you are in a different directory. If inside the script you are writing a file to a relative path that might make a difference for example.

./apps/scripts/script.sh says does does recognize directory or script name.

But

pushd "/apps/scripts"
./scripts.sh
popd

works fine. I'm baffled

Why are you baffled. You have two VERY different paths to process:

./apps/scripts/script.sh

is a relative pathname. The code:

pushd "/apps/scripts"
./scripts.sh
popd

is logically equivalent to the absolute pathname:

/apps/scripts/./scripts.sh

These two paths are equivalent only if you are sitting in the directory / when you use the relative pathname.

And, even if you correctly specify a pathname of a script when invoking it, whether or not it will work depends on whether or not that script uses any relative pathnames when referencing files it wants to open. If you invoke the script with:

/apps/scripts/./scripts.sh

or, more simply,

/apps/scripts/scripts.sh

any relative pathname references scripts.sh makes will be relative to the directory you are sitting in when you invoke scripts.sh .
But, if you invoke the script with:

pushd "/apps/scripts"
./scripts.sh
popd

any relative pathname references scripts.sh makes will be relative to the directory /apps/scripts .

Don -

That makes perfect sense. Thank you for the explanation!!!

Hi one last question.

Would would be best practice then?

Is pushd/popd method okay or would you recommend another method?

Thank you!