Search code examples
macosmacos-big-surdarwin

Alternative to dirname command that supports spaces


Consider the following output, when run the command:

$ /usr/libexec/java_home -v 1.8
/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home

The command dirname are not getting right the directory name:

$ echo $(dirname $(/usr/libexec/java_home -v 1.8))                                                                                     
/Library Plug-Ins/JavaAppletPlugin.plugin/Contents

I think dirname nor supports spaces. I'm behind macOS with Big Sur.


Solution

  • After the tip of @Shawn the solution is simple: just use quotes...

    $ echo "$(dirname "$(/usr/libexec/java_home -v 1.8)")"                                                                                
    /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents
    

    Thanks @Shawn!