11

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).

1
  • 2
    Is xdg-open file.ext an option? Commented Nov 29, 2012 at 14:28

3 Answers 3

16

Mostly close to this is xdg-open:

$ xdg-open somefile.ext
Sign up to request clarification or add additional context in comments.

1 Comment

How to change and view associations: askubuntu.com/questions/62585/…
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

Comments

1

On your .profile

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

and your bash use this editor

5 Comments

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.
You can use at Bash at any time. Is for that session.
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.
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.
In that case editor was TextMate: -w, --wait Wait for file to be closed by TextMate.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.