9

How do I open a file my script generated with the default GUI editor with bash?

On OS X there is the command open, but as far as I know that doesn't exist on linux. What is a good cross-platform alternative?

(executing open somefile.ext on OS X does the same as if I double clicked the file in Finder).

Jawap
  • 2,463
  • 3
  • 28
  • 46

3 Answers3

15

Mostly close to this is xdg-open:

$ xdg-open somefile.ext
Slava Semushin
  • 14,904
  • 7
  • 53
  • 69
5

On linux you have kde-open and gnome-open for specific desktop environments, and xdg-open is more generic but must still be run from a DE.

On windows, (obviously not bash but cmd.exe), I believe the similar command is start.

With bash a cross-platform code could be:

if which xdg-open &> /dev/null; then
    xdg-open $file       # linux
else
    open $file           # mac
fi
Antoine
  • 13,494
  • 6
  • 40
  • 52
1

On your .profile

export EDITOR='~/bin/mate -w'

and your bash use this editor

Kamber
  • 89
  • 3
  • This implies you have to edit `.profile` of each user, and somehow keep it synchronized with the default application defined in the desktop file-association database. – Antoine Nov 29 '12 at 14:48
  • You can use at Bash at any time. Is for that session. – Kamber Nov 29 '12 at 14:58
  • If you're writing this for yourself, sure - you could even hardcode your editor in the script. If it's for other users, I don't see how you can assert that `mate` is what they want. On my system your path isn't even valid. – Antoine Nov 29 '12 at 15:03
  • what does the "-w" indicate? It does have the effect I'm looking for (aka, it writes the file) but it forces a bunch of new windows to open up when using Atom which is highly distracting. – ken Nov 04 '15 at 10:26
  • In that case editor was TextMate: -w, --wait Wait for file to be closed by TextMate. – Kamber Dec 29 '16 at 14:26