3

I have some shell scripts that need to access paths under /Volumes/ e.g. /Volumes/MyDisk/tmp/foo.json. Right now the scripts have hardcoded volume names. I would like to make them portable so they run correctly on my various machines, all of which have different disk names for the boot volume.

Can anyone post a reliable method for getting the disk name of the system on a modern macOS system? (13.0.1 as of this writing)

4
  • 1
    Why not access it directly, under /? For the system volume, all that's under /Volumes is a symbolic link to that anyway (run ls -l /Volumes to see what I mean). Commented Nov 30, 2022 at 19:21
  • Because on a system with multiple attached disks (USB, Thunderbolt, etc) there will be multiple entries under /Volumes @GordonDavisson
    – luckman212
    Commented Nov 30, 2022 at 20:35
  • 1
    But / will always be the system volume. That is, /Volumes/WhateverTheStartupVolumeIs/path/to/some/file is equivalent to just /path/to/some/file. So why bother with the /Volumes/WhateverTheStartupVolumeIs part? Commented Nov 30, 2022 at 21:08
  • @GordonDavisson Might be an edge case, but when booted into Recovery mode, you can't reference the files using just / — you must use a prefix like /Volumes/volName/...
    – luckman212
    Commented Nov 30, 2022 at 23:13

2 Answers 2

5

One possible answer is given below.

function bootVolName() {
  diskutil info / | sed -n 's/^ *Volume Name: *//p'
}

An example is given below.

$ bootVolName
MyDisk

This was tested using High Sierra installed on JHFS+ and Catalina installed on a APFS.

3
  • I don't have a non-English system to test with, but searching specifically for Volume Name might not work on non-English systems? I think using plutil to parse the XML is probably more robust. Also, using hardcoded / instead of bless means the script could return the wrong info during the time between switching the boot drive via System Prefs up until the system is actually rebooted.
    – luckman212
    Commented Nov 30, 2022 at 23:18
  • @luckman212 Are you looking for the name of the volume you're currently booted from, or what's selected as the default startup volume (e.g. in System Preferences)? I (and I think David) assumed it's the first, but you seem to be talking about the second. Commented Dec 1, 2022 at 0:04
  • My mistake. I thought you were referring to the volume name with respect to the current running macOS. Your question instead is acting for the current default boot volume. I edited your question to make this more clear. Commented Dec 1, 2022 at 0:49
2

Not sure if there's a more efficient way, but this gets the job done without requiring any third-party tools:

function bootVolName() {
  diskutil info -plist "$(bless --getBoot)" |
  plutil -extract VolumeName raw -- -
}

$ echo $(bootVolName)
==> MyDisk
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .