Ubuntu – Differences between fish and bash to pass commandline arguments to alias functions

aliasbashcommand linefish

From the answers to my other question here i learned about the possibility to pass commandline arguments to a alias function in Bash.

In Fish i can edit an alias by editing the file config.fish in ~/.config/fish directory and adding a line like this

alias lsp='ls -ah --color=always | less -R;'

and it works perfectly. This should be the equivalent to editing ~/.bash_aliases in bash

But when i try to setup an alias function to pass arguments like this

alias lsp='_(){ ls -ah --color=always $* | less -R; }; _'

it doesn't work for fish?

Are there any differences between fish and bash in the way to setup an alias to pass commandline arguments that prevent this second alias from working with fish instead of bash?

Best Answer

This second command is actually a small bash-script. Bash is the programming language built into the shell. It's used by other shells, but not necessarily, as we see here.

Fish defines a completely different programming language to bash, though it does support simple aliases in the usual alias name=command form; You should define a function instead. This is how you would define the above function in fish:

stefano@lenovo ~> function lsp
                      ls -ah --color=always $argv | less -R
                  end

and funcsave lsp so save it permanently.

You can now run the expected commands, like lsp, lsp -R, lsp *.png* and so on.

You can also 'invoke' bash to run a script for you, using the 'sh' program (this will run 'dash' to be precise). But if you're using fish, chances are you want to use the methods it provides. I've just tested it for the first time, and I'm very impressed so far.

The functions are stored as a file in .config/fish/functions/ in your home directory. The file name will be, in this example, lsp.fish. You can either edit the file, restarting fish afterwards, or just define and save the function again.

The best way to learn fish is by reading its built-in help. From within fish, just type

help

and you'll get a very nicely formatted, extensive and easy to read manual. Actually, this opens the w3m web browser, because the help is in html format:

alt text

(press q-y to exit)