0

I want to have single list of aliases that I source to my .zshrc on all my computers and sync. But on one computer I have docker installed, and on another I don't. And every time I open terminal get an error:

/home/ubuntu/scripts/zsh_aliases:2: command not found: docker

... because I use docker in some aliases.

How do I suppress this inspection or redirect errors to log file?

It turned out this alias produce an error: alias all.restart="docker restart $(docker ps -q)". I guess it is because of $(), but why?

5
  • 2
    It looks like you're invoking an alias, in order to generate that "command not found" error. Could you post the relevant portion of that file?
    – Jeff Schaller
    Commented Nov 7, 2019 at 20:07
  • @JeffSchaller, I added source ~/scripts/zsh_aliases to .zshrc. And alias isalias d="docker"
    – ogbofjnr
    Commented Nov 7, 2019 at 20:24
  • 1
    That alone wouldn't do it; something is calling docker (or d) somewhere...
    – Jeff Schaller
    Commented Nov 7, 2019 at 20:36
  • @JeffSchaller, turned out this alias produce an erroralias all.restart="docker restart $(docker ps -q)" I guess it is because of ```$()``, but why?
    – ogbofjnr
    Commented Nov 7, 2019 at 20:46
  • @ogbofjnr instead of using command -v docker, you could just escape the substitution : alias all.restart="docker restart \$(docker ps -q)". this way, the docker ps part would be re-executed every time you run all.restart
    – matan h
    Commented Sep 20, 2023 at 15:00

1 Answer 1

1

The line that invokes docker is producing that "command not found error"; from the comments, you have a line:

alias all.restart="docker restart $(docker ps -q)"

The $(docker ...) command substitution is invoking docker, which triggers your error when run on systems without docker.

Simply preface the alias with a check for the existence of the docker command:

command -v docker > /dev/null && alias all.restart="docker restart $(docker ps -q)"

The command command will fail if the docker command is not found, which will bypass the subsequent && alias portion. If the docker command does exist, then the alias command is executed.

You must log in to answer this question.

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