4 ms·
$0 is the path of the script. ${0%/* } takes $0, deletes the smallest substring matching /* from the right (the filename) and returns the rest, which would be t
by DangerousPie 12y ago
$0 is the path of the script. ${0%/* } takes $0, deletes the smallest substring matching /* from the right (the filename) and returns the rest, which would be the directory of the script. So this changes the directory to the directory the script is located in.
- rjgray 12y agoIs there any good reason to use this approach over the more readable 'dirname $0'? Not that it would have made any difference to the bug in question of course.
- barrkel 12y ago$0 is not the path of the script. $0 is the path passed to the shell used to execute the script. If run via the #! line, it will contain a path of some kind. But if it's passed directly using "bash myscript.sh", then it won't. And yes, dirname is a way out of this. I'd do this: "$(cd "$(dirname "$0")"; pwd)" if I wanted the path to the script. I would also sanity-check the path by testing for the existence of some files or directories that are expected to exist under it, before trying to delete it all.