I am writing a shell script that is meant to prompt the user for a file, and since I do not expect the end user of my script to be tech-savvy, rather than starting command line for interaction, I would like the shell to be able to summon the system File manager in a standard way (e.g. using xdg or similar) that would open it in an "Open file..."-like dialog, and output the full or relative path to a file or directory once one was selected. Is there a way to do this either in a Linux or MacOS, or both?

  • Please edit your question and gives us more detail. You mention both Linux and macOS (OSX was renamed macOS years ago). Does that mean it needs to work on both systems? Does xdg-open do what you want? Why is the file manager needed? Will the user give a full path? A file name? Why would you want to prompt the user instead of reading arguments from the command line so they can use tab completion?
    – terdon
    Commented May 3 at 10:28
  • yes, it needs to work on both systems, since although i developed it on linux, it will primarily be used on mac computers, which is why I tag both. and yes, the file manager is needed because i do not expect the end user to interact with my script over command line, and, other than writing my own graphical interface, file manager seems like the obvious solution for my use case. the user only needs to select the file in the manager: manager itself should preferably output the full path. Thank you for pointing out the OSX discrepancy.
    – fja
    Commented May 3 at 17:36
  • You won't be able to finx a cross-platform method for this, since macOS and Linux have completely different GUI layers. On macOS, you'll probably need to use AppleScript via the osascript command, none of which exists on any Linux. Commented May 3 at 18:32
  • thats most certainly a shame, but much better than nothing: i should probably reformulate my question to imply that singular solution is not required, and that either or both will suffice.
    – fja
    Commented May 3 at 18:39

1 Answer 1


You can do this on macOS by using the osascript command to run AppleScript's choose file command. Something like this:

if filepath=$(osascript -e 'POSIX path of (choose file with prompt "Pick a file:")'); then
    echo "Your file is: $filepath"
    ls -l "$filepath"
    echo "Cancelled."

Note that if the user cancels the selection dialog (or something else goes wrong), osascript will print an error message to stderr (as well as exiting with an error status, which is what the if test above checks), so you may want to redirect error output to suppress or capture this.

Also, macOS file names and paths often contain spaces, so be sure to put double-quotes around all your variable references (e.g. ls "$filepath" rather than just ls $filepath). This is good scripting hygiene in general, but particularly important here. shellcheck.net is good at spotting common mistakes like unquoted variable references, so I recommend running your scripts past it to see what it recommends.

For more info about the choose file command, see Apple's Mac Automation Scripting Guide: Prompting for Files or Folders.

  • Marked as answer. that last link is exactly what I was looking for! thank you, and will add to this if I figure out a linux portable solution.
    – fja
    Commented May 4 at 15:50

You must log in to answer this question.

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