Issue
In Bash, if VAR="/home/me/mydir/file.c"
, how do I get "/home/me/mydir"
?
id='dv4'>
Solution
dirname
and basename
are the tools you're looking for for extracting path components:
$ VAR='/home/pax/file.c'
$ DIR="$(dirname "${VAR}")" ; FILE="$(basename "${VAR}")"
$ echo "[${DIR}] [${FILE}]"
[/home/pax] [file.c]
They're not internal bash
commands but they are part of the POSIX standard - see dirname
and basename
. Hence, they're probably available on, or can be obtained for, most platforms that are capable of running bash
.
Answered By - paxdiablo Answer Checked By - Timothy Miller (WPSolving Admin)