I used to use CShell (csh), which lets you make an alias that takes a parameter. The notation was something like
alias junk=\"mv \\\\!* ~/.Trash\"
Functions are indeed almost always the answer as already amply contributed and confirmed by this quote from the man page: "For almost every purpose, aliases are superseded by shell functions."
For completeness and because this can be useful (marginally more lightweight syntax) it could be noted that when the parameter(s) follow the alias, they can still be used (although this wouldn't address the OP's requirement). This is probably easiest to demonstrate with an example:
alias ssh_disc='ssh -O stop'
allows me to type smth like ssh_disc myhost
, which gets expanded as expected as: ssh -O stop myhost
This can be useful for commands which take complex arguments (my memory isn't what it use t be anymore...)
An alternative solution is to use marker, a tool I've created recently that allows you to "bookmark" command templates and easily place cursor at command place-holders:
I found that most of time, I'm using shell functions so I don't have to write frequently used commands again and again in the command-line. The issue of using functions for this use case, is adding new terms to my command vocabulary and having to remember what functions parameters refer to in the real-command. Marker goal is to eliminate that mental burden.
The question is simply asked wrong. You don't make an alias that takes parameters because alias
just adds a second name for something that already exists. The functionality the OP wants is the function
command to create a new function. You do not need to alias the function as the function already has a name.
I think you want something like this :
function trash() { mv "$@" ~/.Trash; }
That's it! You can use parameters $1, $2, $3, etc, or just stuff them all with $@
As has already been pointed out by others, using a function should be considered best practice.
However, here is another approach, leveraging xargs
:
alias junk="xargs -I "{}" -- mv "{}" "~/.Trash" <<< "
Note that this has side effects regarding redirection of streams.
Once i did some fun project and i still use it. It's showing some animation while i copy files via cp
command coz cp
don't show anything and it's kind of frustrating. So i made this alias
alias cp="~/SCR/spiner cp"
And this is the spiner script
#!/bin/bash
#Set timer
T=$(date +%s)
#Add some color
. ~/SCR/color
#Animation sprites
sprite=( "(* ) ( *)" " (* )( *) " " ( *)(* ) " "( *) (* )" "(* ) ( *)" )
#Print empty line and hide cursor
printf "\n${COF}"
#Exit function
function bye { printf "${CON}"; [ -e /proc/$pid ] && kill -9 $pid; exit; }; trap bye INT
#Run our command and get its pid
"$@" & pid=$!
#Waiting animation
i=0; while [ -e /proc/$pid ]; do sleep 0.1
printf "\r${GRN}Please wait... ${YLW}${sprite[$i]}${DEF}"
((i++)); [[ $i = ${#sprite[@]} ]] && i=0
done
#Print time and exit
T=$(($(date +%s)-$T))
printf "\n\nTime taken: $(date -u -d @${T} +'%T')\n"
bye
It's look like this
Cycled animation)
Here is the link to a color script mentioned above. And new animation cycle)
For taking parameters, you should use functions!
However $@ get interpreted when creating the alias instead of during the execution of the alias and escaping the $ doesn’t work either. How do I solve this problem?
You need to use shell function instead of an alias to get rid of this problem. You can define foo as follows:
function foo() { /path/to/command "$@" ;}
OR
foo() { /path/to/command "$@" ;}
Finally, call your foo() using the following syntax:
foo arg1 arg2 argN
Make sure you add your foo() to ~/.bash_profile
or ~/.zshrc
file.
In your case, this will work
function trash() { mv $@ ~/.Trash; }