Return filename or directory portion of pathname.
Syntax dirname string
Print to standard output all but the final slash-delimited component of a string (presumably a filename) i.e. Delete the filename portion.
MacOS is not case sensitive and dirname does not consult the file system to see if the case is correct or even that the path/file string exists at all.
If string is a single component, dirname prints . (meaning the current directory).
dirname will exit 0 on success, and >0 if an error occurs.
Set the shell variable FOO to /usr/bin:
FOO=`dirname /usr/bin/trail`
Extract the path from the variable pathnamevar and store in the variable result using parameter expansion $( )
$ result=$(dirname "$pathnamevar")
Set the variable $BASE to the directory where a script is currently saved:
#!/bin/bash
BASE="$(dirname $(realpath $0))"
“Then I came up with this crazy idea just to walk out on the stage with no band at all and just start singing whatever came to mind. I actually fought the idea for a while because it seemed almost too radical, but it became obvious what I was supposed to be doing” ~ Bobby McFerrin
Local man page: dirname - Command line help page on your local machine.
basename – Strip directory and suffix from filenames.
realpath - Return resolved physical path.
ls - List information about file(s).
Equivalent Windows command: Variable Substring