Say I have this script mm in dir1:

#Contents of mm
echo "Hello Main!"

My pwd is dir2, and I now execute mm:

$ ../dir1/mm

There is an executable named hd in both dir1 and dir2. I tried it and the hd in dir2 is executed. Why isn't hd in dir1 executed instead?

  • Is dir2 in your $PATH? Dec 6, 2015 at 3:06
  • No, it's not there.
    – Tosh
    Jan 2, 2016 at 5:43

1 Answer 1


Because execing a file doesn't change the current working directory of your process into the directory of the file.

The executable may choose do it after it's exec'ed. E.g., I sometimes do:

cd "$(dirname "$0")" 

in my script to make sure my current working directory is the directory of the executable, but it's under no obligation to do so.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.