7

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 Answers3

3

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

Mikel
  • 57,299
  • 15
  • 134
  • 153
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.

Steven D
  • 46,160
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.

NES
  • 420
  • 2
  • 7
  • 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 07 '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. – Somebody still uses you MS-DOS Feb 10 '11 at 15:47