For bash script, I can use "$@"
to access arguments. What's the equivalent when I use an alias?
asked Nov 4, 2010 at 21:44
prosseekprosseek8,7481616 gold badges4848 silver badges4444 bronze badges
Alias solution
If you're really against using a function per se, you can use:
$ alias wrap_args='f(){ echo before "$@" after; unset -f f; }; f'
$ wrap_args x y z
before x y z after
You can replace $@
with $1
if you only want the first argument.
Explanation
This creates a temporary function f
, which is passed the arguments.
Alias arguments are only passed at the end. Note that f
is called at the very end of the alias.
The unset -f
removes the function definition as the alias is executed so it doesn't hang around afterwards.
answered May 23, 2017 at 3:17
Tom HaleTom Hale32.8k4242 gold badges161161 silver badges257257 bronze badges
3Aliases are like commands in that all arguments to them are passed as arguments to the program they alias. For instance:
alias ls='ls -l -a -t -r'
# This now executes 'ls -l -a -t -r foo bar'
ls foo bar
If you want to have actual control over how the arguments are interpreted, then you could write a function like so:
my_program_wrapper() {
local first_arg=$1 \
second_arg=$2
shift 2 # get rid of the first two arguments
# ...
/path/to/my_program "$@"
}
answered Nov 4, 2010 at 22:53
amphetamachineamphetamachine5,61722 gold badges3737 silver badges4343 bronze badges
3Adding to the present answers, an important thing to realize about how aliases work is that all the parameters you type after an aliased command will be used literally at the end. So there is no way to use alias for two commands (piped or not), out of which the first should interpret the parameters. To make it clear, here's an example of something that would not work as expected:
alias lsswp="ls -l | grep swp"
(an example inspired by this question) this will always use the output of ls -l
performed in the current directory and do a grep on that - so using
lsswp /tmp/
would be equivalent to ls -l | grep swp /tmp/
and not ls -l /tmp/ | grep swp
.
For all purposes where the arguments should be used somewhere in the middle, one needs to use a function instead of an alias.
ilkkachu147k1616 gold badges263263 silver badges436436 bronze badges
answered Sep 2, 2011 at 12:04
rozcietrzewiaczrozcietrzewiacz40k99 gold badges9797 silver badges104104 bronze badges
1You don't have to do anything, actually; aliases do this automatically. For instance:
$ alias less="less -eirqM"
$ less foo.txt
You will see foo.txt's first page, and less
will quit at EOF (-e), searches will be case-insensitive (-i), etc.
answered Nov 4, 2010 at 21:50
Warren YoungWarren Young73.2k1717 gold badges180180 silver badges171171 bronze badges
2I'm answering for csh:
Yes, you can use the parameters in aliases and - as a difference to what has been said above - you can refer to them anywhere in the definition of alias - not only at the end.
Example for tar-gz -ing something:
$ alias tgz "tar cvf - \!:1 | gzip -9 > \!:2.tar.gz"
, where !:1
and !:2
are the parameters you will supply when calling your alias.
Example of use:
$ ls
clrcf.dat user_comment_2016.06.03_12:51:50.txt user_comment_2016.06.03_12:54:48.txt
TEST-wADM.tec user_comment_2016.06.03_12:52:04.txt user_comment_2016.06.03_12:55:13.txt
$ tgz user* out
a user_comment_2016.06.03_12:51:50.txt 1K
a user_comment_2016.06.03_12:52:04.txt 1K
a user_comment_2016.06.03_12:54:48.txt 1K
a user_comment_2016.06.03_12:55:13.txt 1K
$ ls out*
out.tar.gz
Which effectively means that you used two parameters that you inserted at arbitrary places of the tar command, making of all of it an alias tgz
answered Jul 18, 2018 at 14:41
stevicastevica17311 gold badge33 silver badges88 bronze badges
1This example, to pretty print a markdown file in the terminal, is working if the first command of the pipe is able to read from stdin:
alias mdcat='(pandoc | lynx -stdin -dump) <'
So mdcat myfile.md
can then be used instead of pandoc myfile.md | lynx -stdin -dump
.
This is not exactly answering your question, as it does not make "$@" available in the alias, but it probably answers some real-world usecases for this question.
answered Nov 24, 2021 at 10:34
1 You must log in to answer this question. Protected question. To answer this question, you need to have at least 10 reputation on this site (not counting the
association bonus). The reputation requirement helps protect this question from spam and non-answer activity.
Start asking to get answers
Find the answer to your question by asking.
Ask questionExplore related questions
See similar questions with these tags.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4