Differences between fish and bash to pass commandline arguments to alias functions?

6,782

Solution 1

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)

Solution 2

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:
6,782

Related videos on Youtube

OverStacked
Author by

OverStacked

Updated on September 17, 2022

Comments

  • OverStacked
    OverStacked over 1 year

    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?

  • OverStacked
    OverStacked over 13 years
    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?