I have two aliases watchExpand and l. I know that you can make bash expansion work with aliases by placing a trailing space like so:

alias watchExpand='watch '

l is aliased to ls -larthiF --context. So when I type the command watchExpand l it works like a charm.

However if I supply a parameter to watchExpand command, e.g.

watchExpand -n 1 l

My l alias no longer works. How can I get bash expansion after arguments?

  • aliases don't expand unless they're the first word in a line
    – Jeff Schaller
    Commented Oct 21, 2016 at 18:41
  • 1
    bash reference says: "If the last character of the alias value is a blank, then the next command word following the alias is also checked for alias expansion"
    – Jeff Schaller
    Commented Oct 21, 2016 at 18:43
  • I'm not sure this is a good idea, but if the question is: "how can I expand every parameter to an aliased command in case that parameter is also an alias?" is to make your aliased command (watchExpand) a function that loops over its parameters and does so manually
    – Jeff Schaller
    Commented Oct 21, 2016 at 18:49

3 Answers 3


Here's the bad idea I think you're asking for:

function watchExpand() {
  for param in $@
    if alias $param >/dev/null 2>&1
      exp=$(alias $param | cut -d= -f2| sed -e s/^\'// -e s/\'\$//)
      e+=" $exp"
      e+=" $param"
  watch $e

Zsh has global aliases. You could do:

alias -g @l='ls -larthiF --context'

and then:

watch -n 1 @l

Note that @ is not mandatory, but I use it to avoid having the global aliases invoked unintentionally.


I figure out the solution with my way.

Firstly, i create a function called "addExpand" to add alias/fucntion easier if i want.

xb@dnxb:/tmp/t$ type -a addExpand
addExpand is a function
addExpand () 
    echo -e "#!/bin/bash\nshopt -s expand_aliases\n. ~/.bash_aliases 2>/dev/null\n$1"' "$@"' | sudo tee /usr/bin/e_"$1";
    sudo chmod +x /usr/bin/e_"$1"
xb@dnxb:/tmp/t$ addExpand l
shopt -s expand_aliases
. ~/.bash_aliases 2>/dev/null
l "$@"

After i run addExpand l, alias l will created as an executable file named /usr/bin/e_l with following content:

xb@dnxb:/tmp/t$ cat /usr/bin/e_l
shopt -s expand_aliases
. ~/.bash_aliases 2>/dev/null
l "$@"

Now enjoy to use expand version of alias/function:

xb@dnxb:/tmp/t$ watch --color -n 1 e_l /tmp //works like a charm !!!


[1] e_l, prefix with 'e_' to imply it's expansion version of command.

[2] I feel too heavy to perform sourcing every seconds if run with watch -n 1. I might need to find a way to solve this problem.

[3] Another disadvantage is it doesn't resolve alias recursively.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .