I want to find out what device my file is on so that I can use it in a script. I can get this far:
$ df .
Filesystem 512-blocks Used Available Capacity Mounted on
/dev/disk0s2 498438976 294369520 203557456 60% /
but this output feels too clumsy; is there a better way than parsing this to get the first 'word' of the second line?
What I really need is something like this so I can pipe it to the next command:
$ somecommand .
/dev/disk0s2
How can I achieve this, preferably without resorting to string hacking the 'df' output?
Best Answer
You can do it with the shell alone (works in
bash
,dash
,ksh
,zsh
):Or if output is not needed (result will be kept in $a) and your shell supports process substitution (like
bash
,zsh
):And here are some comparisons with the other solutions' speed:
(Not compared with the
stat
solution as it not works here.)