Bash – watch command alias expansion

aliasbash

If a run the watch command containing an alias, it will not expand the alias. I have tried both with single quote and double quotes, in fact given the following alias:

# alias ll
alias ll='ls -l --color=tty'

The following command will fail

# watch ll
sh: ll: command not found

Shouldn't command line expansion work in this case?

Best Answer

Aliases are only expanded as the first argument, or after another alias with a trailing space on the end of the command.

From bash's help alias:

A trailing space in VALUE causes the next word to be checked for alias substitution when the alias is expanded.

To do this, try the following:

alias watch='watch '
alias ll='ls -l --color=tty'
watch ll

Bear in mind that some versions of watch strip colours by default, on some versions this can be stopped by using --color or -G.

Related Question