I have two aliases:

alias rm='python my/rm/script.py '
alias docker='sudo docker '

The problem is that when running the statement $ docker rm ARGS the keyword rm is being expanded as alias and docker complains that 'python' is not a docker command.

I looked for a couple of solutions. One solution is to use command keyword. Another is to use shopt to toggle alias expansion. I'm unable to put them to good use.

Both of these are not working out for me:

alias docker='shopt -u expand_aliases; sudo docker '
alias docker='command sudo docker '

Unalias works:

alias docker='unalias rm; sudo docker '

The problem with unalias is that after a while I'm gonna forget that my rm alias is no longer in place in a running shell environment wherever my docker commands have been executed. As a result me using rm on a file would instantly erase it, in contrast to my python script which acts as a safetynet and moves to-be-deleted files in Trash. I want to avoid getting into this situation where I mistakenly end-up using GNU rm command and not my rm alias, and that's why I don't want to use unalias.

Another solution is to let go of docker alias. The downside is that I'd have to write sudo keyword for every useful docker command I want to execute. That is a chore which I was avoiding using the alias for docker.

One more solution is to escape rm keyword like this:

docker \rm ARGS

This works. But I'm hoping for a fire-and-forget solution here so that I can execute my docker commands naturally without concerning about anything else.

Given the above constraints, how do I use alias for both rm and docker but prevent rm keyword being expanded as alias in docker commands?

1 Answer 1


The reason your rm alias is expanded when used after your docker alias is because the definition of the docker alias includes a space at the end. Putting a space at the end of the alias definition is the way to ask the shell to apply alias expansion to the next word.

Solution: Define your docker alias without a space at the end. You may also want to remove the space from the end of your other aliases.

You could probably also get rid of your docker alias entirely by adding your user to the docker group, but that's an administrational decision that you take locally.


You must log in to answer this question.

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