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:
(press q-y to exit)
Aliases don't take arguments. With an alias like alias foo='bar $1'
, the $1
will be expanded by the shell to the shell's first argument (which is likely nothing) when the alias is run.
So: Use functions, instead.
d () {
num=${1:-5}
dmesg |grep -iw usb|tail -$num
}
num=${1:-5}
uses the first argument, with a default value of 5 if it isn't provided.
Then you can do:
$ d
# prints 5 lines
$ d 10
# prints 10 lines
Or, if you change the options you used slightly:
alias d="dmesg|grep -iw usb|tail -n 5"
Then you can pass additional -n
options:
$ d
# prints 5 lines
$ d -n 10
# prints 10 lines
If multiple -n
options are specified for tail
, only the last is used.
Best Answer
No, you can't pass arguments to an alias. But you can define a function:
(you can put it all in one line if you wish, just mind the semicolon at the end of the command)
This will work as intended and is the recommended idiom that should replace aliases for all but trivial purposes.
But maybe what you really need is a Makefile?