Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why doesn't the "watch" command work on an alias?

So I’ve made my customized alias command and tried to use it with alias, but my alias was not recognized while concatenated with the watch command.

So I tried to make a thread of it and fortunately, this one helped me out.

But what is the reason for this?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I’ve created an alias in my .bashrc which works perfectly fine.

alias gpu='sensors nouveau-pci-0100'
alias cpu='sensors coretemp-isa-0000'

and when I concatenate watch and gpu like this:

watch GPU

I get this below every 2 seconds:

sh: 1: gpu: not found

And I solved it regarding the mentioned thread above like this:

alias = watchh='watch '
watchh gpu

But why does this happen? Why can’ It use my surely defined alias command?

My guess is it is something about the user because I’ve gone root once and I couldn’t use my alias but I surely need an expert to answer this.

>Solution :

It’s nothing to do with users.

Aliases are only expanded in the interactive shell for which they are defined – so an interactive bash shell if you defined them via ~/.bashrc, or an interactive zsh shell if you define them in ~/.zshrc for example.

The watch command invokes commands via a non-interactive /bin/sh shell.

By aliasing watch itself, as alias watchh='watch ' (with a trailing space) and then using watchh gpu, you force the current interactive shell to expand gpu before it’s passed to watch.

Note that in zsh, aliases may be defined as global which allows them to be expanded anywhere in a command – avoiding the need to alias watch with a trailing space.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading