Clement
Clement

Reputation: 23

alias zsh: parse error near `do'

I'm facing an issue with an alias for zsh.

I've this alias in my .zprofile file: alias cleanimg="for i in `docker images | grep '<none>' | awk '{print $3}'`; do; docker rmi -f $i; done".

When running cleanimg from my terminal I'm getting this error: zsh: parse error near `do'.

I tried to remove the ; right after the do keyword but it didn't fix the issue. However, that alias runs correctly if I execute the code directly from a terminal.

Can anybody help me with this syntax error?

Thanks by advance.

Upvotes: 2

Views: 9178

Answers (3)

chepner
chepner

Reputation: 531868

Don't try to use an alias for this; use a function.

cleanimg () {
  for i in $(docker images | awk '/<none>/ {print $3}'); do
    docker rmi -f "$i"
  done
}

This saves you from having to get the quoting right so that the command substitution runs when the alias is used, rather than when it is defined.

(Also, grep | awk pipelines can almost always be implemented using awk alone.)

(I also wonder if you can dispense with awk and the loop using the --filters option instead; maybe docker rmi -f $(docker images -q -f "dangling=true")?)

Upvotes: 6

felixyadomi
felixyadomi

Reputation: 3406

If all you want is to clean your docker images, you can set an alias like this:

alias cleanimg='docker rmi -f $(docker images -q -f "dangling=true")'

The sub commande docker images -q -f "dangling=true" return all your id to remove and since docker rmi accept arguments you can pass them directly

Upvotes: 0

Yaron Idan
Yaron Idan

Reputation: 6765

You have an extra semicolon in your for loop, just fix it to -
alias cleanimg="for i indocker images | grep '' | awk '{print $3}'; do docker rmi -f $i; done"

Upvotes: -1

Related Questions