Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

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.

For some other situations where this is inconvienient, you might want to use an array instead, for example:

ll=(ls -l --color=tty)

# Each of the program arguments are passed separately to watch
watch "${ll[@]}"
# All of the program arguments are passed as one argument to watch
watch "${ll[*]}"
share|improve this answer
    
This is absolutely awesome answer! Thanks and +1 of course. –  ztank1013 Nov 25 '11 at 12:47

I thought here basic problem is while executing watch, the argument is given to "sh -c" which means if alias ll is not defined in sh(dash shell's) environment then it will not expand it. But I was wrong and this is not the case, Chris' answer above is right.

share|improve this answer
1  
Not exactly. Try watch 'alias ll="ls -l --color=tty"; type ll; ll'. –  Chris Down Nov 25 '11 at 12:54
    
+1 for immediate response to verify it. I was wrong above. –  Sachin Divekar Nov 25 '11 at 12:58
    
@ChrisDown, so even if watch uses "sh -c" the your explanation is applicable for sh too. Am I right? –  Sachin Divekar Nov 25 '11 at 13:02
1  
That depends on what shell is being used as sh. Notice that this does work: sh -c $'alias ll="ls -l --color=tty"\ntype ll\nll' –  Chris Down Nov 25 '11 at 13:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.