Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 3 down vote accepted

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)

share|improve this answer
    
thank you very much. can these functions edited subsequent, once there already stored? where are they stored? P.S. did you found any good tutorial on this, the webpage with the documentation for fish is since several days unavailable? –  NES Jan 2 '11 at 15:41
    
thanks for the update, wasn't aware of this. –  NES Jan 2 '11 at 16:19
    
Actually, in fish 'alias' is just a shellscript wrapper around the function builtin. See ridiculousfish.com/shell/user_doc/html/commands.html#alias –  user10962 May 27 '13 at 12:02

I've never seen a contraption like this before.

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

An alias that declares a function and runs it. You should just make a function in the first place. In bash, functions supersede aliases in almost every way.

lsp() { ls -ah --color=always "$@" | less -R; }

Put that in your ~/.bashrc file, or if you prefer to put functions in a separate file, like ~/.bash_functions then you can do that, but you have to source that file from ~/.bashrc

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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