View Full Version : Extract command output to a variable?

09-03-2004, 07:29 PM
In a thread on the System forum, Dale Mox very generously provided the way to get the diskname of a device (so that I could unmount the diskname from an Applescript), but he asked for help in one remaining problem, which is getting the diskname into a variable. I wonder if anyone in this forum can help with the question?

Here (cut on the dotted line) is Dale's message:


This will give you the diskname of the device (Given the name Silvery) :
mount | grep "Silvery" | cut -f1-1 -d " " | cut -f3-3 -d "/"
Another example:
mount | grep "I dont know" | cut -f1-1 -d " " | cut -f3-3 -d "/"

so set that to a variable, say devname and then use the command:
hdiutil detach $devname

Only thing is that I don't know how to set the output of a command to a variable... any help?
I tried this:
devname = 'mount | grep Silvery | cut -f1-1 -d " " | cut -f3-3 -d "/"'
but that was the string not the output
then I tried this:
mount | grep Silvery | cut -f1-1 -d " " | cut -f3-3 -d "/" > pbcopy
but that doesn't work


Is there any way to get that diskname output from the mount command into a variable? Any help will be gratefully received.

BTW, here is a link to the earlier thread:


Edward Mendelson

09-03-2004, 08:31 PM
Two problems I can see. You need to use ` rather than ' to quote the command, otherwise it never gets executed. Also, lose the spaces around the "="; they'll kill you.

devname=`mount | grep Silvery | cut -f1-1 -d " " | cut -f3-3 -d "/"`
echo $devname

See if that gives you what you need.

09-03-2004, 11:32 PM
Absolutely perfect, jbc. Thank you!! I can now work this into a hint that may be useful to a few people (certainly to me)...

Best wishes,

Edward Mendelson