73

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?

110

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.

  • 1
    This is absolutely awesome answer! Thanks and +1 of course. – ztank1013 Nov 25 '11 at 12:47
  • 1
    Thanks... For all this time, I used to 1. type ll, 2. press ctrl+alt+e, 3. then prefix watch. This is a time saver. – anishsane May 10 '16 at 8:02
  • 14
    Unfortunately this method has a limitation that you cannot provide options to watch because the alias has to be the next word, whereas watch needs its arguments before the command to run. If you tried watch -n 10 <alias>, bash won't expand the alias. To work around it, you need an alias that includes the options you want (e.g., `alias watch-10='watch -n 10 '), still with a trailing space. – indiv Jul 10 '17 at 19:46
2

Maybe we could manually expand the alias before watch sees it?

watch $(alias ll | cut -d\' -f2)

Explanation

The output of alias ll looks like:

$ alias ll
alias ll='ls -lAGh'

So we set cut's delimeter to be single quote, and cut the 2nd field, leaving:

ls -lAGh

ie the expanded alias. That then forms the args given to watch.

Make a function to do it

function watcha {
    watch $(alias "$@" | cut -d\' -f2)
}

Then,

watcha ll

works as desired. This is awful and will fail in all sorts of situations. Sorry.

0

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.

  • 2
    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
0

Have watch run a new interactive shell, which will expand the alias:

watch -x bash -i -c ll

(The -x is intended to stop watch from using it's own sh -c to wrap our given command. We don't want that because we need to pass -i to sh or bash, to make it an interactive shell, which reads .bashrc and defines aliases. With -x, watch executes the command using exec.)

Gah, actually, this doesn't work. After displaying the output of 'll' once, then sleeping 2 seconds, the job gets backgrounded, with the slightly enigmatic:

1]+  Stopped                 watch -x bash -i -c ll

Why did it stop? Where did the open brace [ go? Darn I thought this would be a great answer but I can't make it work.

  • Yeah it can call the alias though it stops afterward – Nam G VU Jan 25 at 15:52
0

There's a custom program to handle this over at github.com/antonmedv/watch. It supports multiple shells. Of course because it's custom software to install it's OK for your normal computers but not helpful on random servers you might be connected to. Here's how I run it so I can watch my zsh aliases and functions:

function watch-zsh() {
  WATCH_COMMAND='zsh -ci' /usr/local/bin/watch "$@"
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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