Article 596WB Opening Windows files from bash and eshell

Opening Windows files from bash and eshell

by
John
from John D. Cook on (#596WB)

I often work in a sort of amphibious environment, using Unix software on Windows. As you can well imagine, this causes headaches. But I've found such headaches are generally more manageable than the headaches from alternatives I've tried.

On the Windows command line, you can type the name of a file and Windows will open the file with the default application associated with its file extension. For example, typing foo.docx and pressing Enter will open the file by that name using Microsoft Word, assuming that is your default application for .docx files.

Unix shells don't work that way. The first thing you type at the command prompt must be a command, and foo.docx is not a command. The Windows command line generally works this way too, but it makes an exception for files with recognized extensions; the command is inferred from the extension and the file name is an argument to that command.

WSL bash

When you're running bash on Windows, via WSL (Windows Subsystem for Linux), you can run the Windows utility start which will open a file according to its extension. For example,

 cmd.exe /C start foo.pdf

will open the file foo.pdf with your default PDF viewer.

You can also use start to launch applications without opening a particular file. For example, you could launch Word from bash with

 cmd.exe /C start winword.exe
Emacs eshell

Eshell is a shell written in Emacs Lisp. If you're running Windows and you do not have access to WSL but you do have Emacs, you can run eshell inside Emacs for a Unix-like environment.

If you try running

 start foo.pdf

that will probably not work because eshell does not use the windows PATH environment.

I got around this by creating a Windows batch file named mystart.bat and put it in my path. The batch file simply calls start with its argument:

 start %

Now I can open foo.pdf from eshell with

 mystart foo.pdf

The solution above for bash

 cmd.exe /C start foo.pdf

also works from eshell.

(I just realized I said two contradictory things: that eshell does not use your path, and that it found a bash file in my path. I don't know why the latter works. I keep my batch files in c:/bin, which is a Unix-like location, and maybe eshell looks there, not because it's in my Windows path, but because it's in what it would expect to be my path based on Unix conventions. I've searched the eshell documentation, and I don't see how to tell what it uses for a path.)

More shell posts

The post Opening Windows files from bash and eshell first appeared on John D. Cook.

dPfcRUGtbKg
External Content
Source RSS or Atom Feed
Feed Location http://feeds.feedburner.com/TheEndeavour?format=xml
Feed Title John D. Cook
Feed Link https://www.johndcook.com/blog
Reply 0 comments