Pass Command Line Arguments To a Bash Alias Command

by on October 16, 2007 · 25 comments· LAST UPDATED April 7, 2012

in , ,

How do I pass all command line args to my bash alias called foo. For example:

alias foo="/path/to/command $@"

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 file.

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 25 comments… read them below or add one }

1 Steve Lessard July 15, 2010 at 4:22 pm

Where do I put that function definition? Are you suggesting that I define it like this?

alias foo=”function foo() { /path/to/command “$@” ;}”

Reply

2 Mike Smith August 2, 2010 at 2:02 pm

If you are using ‘bash’ for instance… you’d add something like this to .bashrc in your home directory.

function check_config () {
su -c “/var/lib/rancid/bin/clogin $@” -l rancid -s /bin/bash;
}

Then re-source your .bashrc via msmith@rancid:~$ . ~/.bashrc

Reply

3 Joe C October 3, 2010 at 8:39 am

Thank you Vivek for this!!!

Steve,
I add this into .profile (in the home dir of my mac) and it works like a charm:
function s8080(){ /Applications/mamp8080/ctlscript.sh “$@”;}

Thus, the “alias” becomes s8080 (i.e. the function name is the “alias”).

-Joe

Reply

4 alex March 5, 2011 at 2:05 pm

Although old this seemed to be a top result for alias arguments, so thought I’d add my 2cents;

In my .bashrc I’ve defined functions directly – there’s no need to reference a shell script, so I’ve got

function chrome {
google-chrome http://www.google.com/search?q=“$1″
}

Which means when I type;

chrome “any phrase”

into a terminal it opens a google chrome window with the search result for “any phrase”. Fairly handy…

Reply

5 rtdp April 4, 2011 at 4:17 pm

yes, this solved prob for me as well.. thanks.

my bit of code –
function task_add(){
echo “$@” >> tasklist;
}

Reply

6 Wicker July 6, 2011 at 6:01 pm

I come for a FreeBSD environment using csh. In it I can do an alias like this:

qp grep -h -i !^ /var/log/qpopper

and type qp username to output the found lines of the qpopper file.

I can’t seem to find something similar in bash.

Got any ideas? Thanks.

Reply

7 christophe August 26, 2011 at 2:38 pm

alias qp=”xargs -I {} bash -c ‘grep -h -i {} /var/log/qpopper’ <<<"

Reply

8 christophe August 25, 2011 at 8:30 am

alias foo=”xargs -I {} bash -c ‘/bin/find {}’ <<<"

Reply

9 christophe August 25, 2011 at 8:58 am

example:

foo ‘-name “\*.eps”‘

returns
./a.eps
./b.eps

Reply

10 anonymous penguin September 7, 2011 at 5:15 pm

Thanks a lot for this. I was trying to pipe whois output to less, and this did the job:

function w1() {
/usr/bin/whois “$@” | less;
}

Reply

11 slmingol October 21, 2011 at 6:37 pm

You don’t have to use a function to do this. You just need to escape the $@ when you’re setting this with the alias command like so:

% alias myecho="echo \$@"
myecho "hello world"
hello world
%

Reply

12 waffle December 20, 2011 at 11:04 pm

@slmingol: I think it does have to be a function and the “myecho” command you list doesn’t work. If you try this –

alias myecho=”echo pre\$@post”
myecho “hello world”
prepost hello world

You can see that it’s just postpending the actual “hello world” at the end. You can do this type of thing in tcsh for sure, but not sure how to do it in bash. Here’s the tcsh representation –

alias myecho ‘echo pre\!:1post’
myecho “hello world”
prehello worldpost

And the bash function (you might need to unalias “myecho” before declaring this function… aliases seem to take precedent over functions, even if the function is declared after the alias) –

function myecho() { echo pre$@post; }
myecho “hello world”
prehello worldpost

Is there a way to do this in bash with aliases? Thanks for the thread, very helpful.

Reply

13 Rick M August 18, 2012 at 2:17 pm

Is there anyway to do what you describe above but have the command run in the background?

% alias mycmd=”/usr/bin/doit \$@”

When I enter ‘mycmd’ I want ‘doit’ to run in the background

Reply

14 N00b February 3, 2014 at 10:57 am

This doesn’t seem to work:
alias hg=”if [ \$# = 0 ]; then history; else history|grep \$@ ; fi”
hg works but hg world results in an error “bash: syntax error near unexpected token `world’.

Reply

15 bruce.ouyang November 28, 2011 at 4:46 am

thanks for code, it works.@slmingol

Reply

16 jimyac February 4, 2012 at 3:32 am

Why would you define an alias to point to a local function at all? It’s just one more redirect. Just calling the function waffle listed from the command line
function myecho () { echo pre$@post; }

Tip: to list your defined funtions use declare -F (similar to using “alias” to list your aliases)

Reply

17 jimyac February 4, 2012 at 3:33 am

fixing grammar: Just call the function waffle listed from the command line

Reply

18 Anonymous August 30, 2012 at 5:26 pm

Just use single quotes in the alias definition instead of double-quotes and the special variable won’t be evaluated until runtime:
alias foo=’/path/to/command $@’

Reply

19 Martijn November 19, 2012 at 3:47 pm

Won’t work

alias foo=’echo pre $@ post’ will result in:
pre post test test

Reply

20 Martijn November 19, 2012 at 3:48 pm

With command btw:
foo test test

:-P

Reply

21 Sundaram September 21, 2012 at 7:08 pm

Helped a lot! Thanks man :)

Reply

22 Anonymous October 27, 2012 at 1:46 pm

Nice one.

Reply

23 rob July 31, 2013 at 8:14 pm

seems like bash’s alias wants to tack the argument list from the command line to the end of the aliased string. so for instance:

new() { /bin/ls -lt “$@” | head ;}

ends up executing “head *pdf” if i do “new *pdf” from the command line.

while i understand this makes writing simple aliases easier, it complicates this case. how do i prevent bash from constructing the command line with the args tacked onto the end?

Reply

24 kevin December 6, 2013 at 12:14 am

Hello All,

I try pass the argument from command line to my alias path.
Here’s example –

alias mypath=”/home/bin/Linux{32|64}-CentOS{5.10|6.4}-{buildID}.nightlyBuilds_{buildID}

I’d like pass argument on the command line for those variable in curly bracket
namely;
mypath “32″ “6.4″ “7.1.2″

Thanks
Kev

Reply

25 waffletower March 4, 2014 at 11:29 pm

Sad that bash doesn’t have the escape facilities of tcsh… Long live tcsh!

Reply

Leave a Comment

Tagged as: , , ,

Previous Faq:

Next Faq: