Quote:
Originally Posted by tkmsr
Want to know what dirname does I read the man page dirname stores name of parant directory is what I understood is it correct?
|
Not really: dirname strips the trailing "/something" from a string (path). It is not aware of the actual parent directory of a file. This means it works even on non-existent paths, e.g.
Code:
$ dirname /pippo/pluto/paperino
/pippo/pluto
Suppose you launch the script posted above as
in this case the dirname command results in
If you want to retrieve the full path of the script, better to use readlink (despite its name it can be forced to retrieve the full path of a file, not only symbolic links). Using a nested command substitution you can retrieve the full path of the parent directory of the script:
Code:
dir=$(dirname "$(readlink -f "$0")")
Regarding the double quotes, they can be useful in this case if the name of the script (hopefully not) or its path contains blank spaces.