Of course, which is what I was trying to warn you about.
Hm. $0 is a special variable, which tells the script how it was run. If you typed in ./programs/myscript.sh then $0 is just that, ./programs/myscript.sh. Or if you typed in /usr/local/sbin/myscript.sh then it gets given that.
So if the first character of $0 is a /, you have the full path in that string and just need to extract it. If it begins with anything else, the string "${PWD}/$0" will contain the full path. So you can extrapolate it from inside the script itself, doesn't need to be fed into it.
---------- Post updated at 09:47 PM ---------- Previous update was at 09:44 PM ----------
First method doesn't "find" anything, it just assumes you're already there.
The second method does the same thing, since "cd $PWD" is a complete no-op. You're already in $PWD, that's what $PWD means.
So you're not finding it, you just happen to already be where you wanted to be...
Extrapolate it from $PWD and $0 as described above instead