Pass Command Line Arguments To a Bash Alias Command

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 "$@" ;}


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.

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 32 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
32 comments… add one
  • Steve Lessard Jul 15, 2010 @ 16:22

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

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

  • Mike Smith Aug 2, 2010 @ 14:02

    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

  • Joe C Oct 3, 2010 @ 8:39

    Thank you Vivek for this!!!

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

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


  • alex Mar 5, 2011 @ 14:05

    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 {

    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…

  • rtdp Apr 4, 2011 @ 16:17

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

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

  • Wicker Jul 6, 2011 @ 18:01

    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.

    • christophe Aug 26, 2011 @ 14:38

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

  • christophe Aug 25, 2011 @ 8:30

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

    • christophe Aug 25, 2011 @ 8:58


      foo ‘-name “\*.eps”‘


  • anonymous penguin Sep 7, 2011 @ 17:15

    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;

  • slmingol Oct 21, 2011 @ 18:37

    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
    • waffle Dec 20, 2011 @ 23:04

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

    • Rick M Aug 18, 2012 @ 14:17

      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

    • N00b Feb 3, 2014 @ 10:57

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

  • bruce.ouyang Nov 28, 2011 @ 4:46

    thanks for code, it works.@slmingol

  • jimyac Feb 4, 2012 @ 3:32

    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)

    • jimyac Feb 4, 2012 @ 3:33

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

  • Anonymous Aug 30, 2012 @ 17:26

    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 $@’

    • Martijn Nov 19, 2012 @ 15:47

      Won’t work

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

      • Martijn Nov 19, 2012 @ 15:48

        With command btw:
        foo test test


  • Sundaram Sep 21, 2012 @ 19:08

    Helped a lot! Thanks man :)

  • Anonymous Oct 27, 2012 @ 13:46

    Nice one.

  • rob Jul 31, 2013 @ 20:14

    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?

  • kevin Dec 6, 2013 @ 0:14

    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
    mypath “32” “6.4” “7.1.2”


  • waffletower Mar 4, 2014 @ 23:29

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

  • andy Aug 30, 2014 @ 15:13


    I’m here on Ubuntu and you have to use an alternate line

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

    so watch out

  • andy Aug 30, 2014 @ 15:14

    ( /bin/find on *Buntu will result in a “file not found error”)

  • smak Apr 22, 2015 @ 9:03

    Hey Guys,
    How do I make a aliaze for this ?
    cli -o set -n tp-to-pr -k 54fa8580 -e 20 -b rc -v 0

  • Lee Oct 30, 2015 @ 14:38

    Thank you!!!!

  • Robert Balejik May 25, 2016 @ 9:52

    I have alias: gedit =’ gedit “$@” & /dev/null & disown %+’
    cause everytime open gedit it “blocks” terminal tab until I close gedit but it when I source .bashrc it says:
    bash: alias: gedit: not found
    bash: alias: = gedit “$@” & /dev/null & disown %+: not found

  • Chan Kim Jun 1, 2016 @ 5:21

    All thanks above my it doesn’t work for me.
    I wanted to make an alias F so that when I type ‘F filename’ it searches for the file filename. I’m on ubunut14.04.4LTS using bash. I made this alias in my .bashrc
    function F() { find . -name “$@”; }
    I resourced it but when I type ‘F’ it give me
    find: paths must precede expression:
    Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path…] [expression]
    what is wrong?

    • Chan Kim Jun 1, 2016 @ 5:25

      I just tried in .bashrc
      alias F=’find . -name $@’
      and it works. See below. no need to define function..
      ckim@abnc:~/prj/abts/abfrcnn/py-faster-rcnn$ F

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum