6

After reading this answer about a zsh feature:

If a given extension has a suffix alias, you can execute a file with that extention directly, and ZSH will launch the given program and pass the filename as an argument.

Is there something similar to bash? That is, if a given file has an extension, how to directly execute that file, automatically launching a program just by calling the file's name? So doing

program /path/to/foo.ext

/path/to/foo.ext

would equivalent to the same zsh feature? Is this feature available in newer versions of bash?

3

If you are on Linux, you could try setting up binfmt misc.

2

While I am not familiar with every feature of Bash, I do not believe this is a built-in feature of the Bash shell. I was unable to find this feature in the relevant sections of the Bash manual.

You may be able to cobble something together using trap. From help trap:

Trap signals and other events.

Defines and activates handlers to be run when the shell receives signals or other conditions.

Thus by using the command:

$ trap my_function ERR

I can ensure that my_function is called whenever a command fails. my_function could be a function that parses the previous command, looking for known extensions and calling the appropriate command based on that extension.

Depending on your interest, writing such a function may be more or less interesting than moving to the z shell.

1
  • Well, I think using this approach with xdg-open pointed out by @akira because of @NES answer is the way to go. Until them, just an alias do xdg-open is good to me. Feb 10 '11 at 15:50
1

You could use fish instead of Bash. It has the functionality you're searching and lots interesting more.

For your example. Just type in

open manual.pdf

and it opens the pdf file with the associated program.

2
  • 1
    so your answer is "no." plus: your 'open' is most likely only working on mac in the way you think it works. on linux it is a symlink to openvt which "starts a program on a new virtual terminal". on linux it would be xdg-open or gnome-open.
    – akira
    Feb 7 '11 at 15:13
  • @akira: Indeed, using xdg-open works in my Ubuntu machine. I think I'll just create an "open" alias to xdg-open. Feb 10 '11 at 15:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.