zappee
zappee

Reputation: 22716

Create a Unix alias with $(...) does not work

I have the following, working Unix command:

docker container stop $(docker container ls -a | awk 'NR>1 {print $1}')

I would like to assign this to an alias, like sc (stop containers) but this command does NOT work:

alias sc="docker container stop $(docker container ls -a | awk 'NR>1 {print $1}')"

It seems that the expression is evaluating somehow while adding it to the alias instead of just assign it.

How can I make it work?

--- UPDATE ---

This command also works fine as a command:

docker container ls -a | awk 'NR>1 {print $1}' | xargs docker container stop

Result:

49b947bb6d61
cb25909f1d58
99bd5a3690c8
147f554934c8

But if I assign it to alias does not work:

alias sc="docker container ls -a | awk 'NR>1 {print $1}' | xargs docker container stop"

Result:

49b947bb6d61
weblogic-admin-server
cb25909f1d58
oracle-db
99bd5a3690c8
weblogic-managed-server-1
147f554934c8
weblogic-managed-server-2
Error response from daemon: No such container: xxxxxxxxxxxxxxxxxx
Error response from daemon: No such container: bin/sh -c '$ORACLE…
Error response from daemon: No such container: 46
Error response from daemon: No such container: hours
Error response from daemon: No such container: ago
Error response from daemon: No such container: Exited
Error response from daemon: No such container: (137)
Error response from daemon: No such container: 45
Error response from daemon: No such container: hours
Error response from daemon: No such container: ago

Upvotes: 0

Views: 84

Answers (2)

Ron
Ron

Reputation: 6611

You need to escape the $ in your .bashrc file

Try this:

alias sc="docker container stop \$(docker container ls -a | awk 'NR>1 {print \$1}')"

or

alias sc="docker container stop \$(docker container ls -aq)"

Upvotes: 1

William Pursell
William Pursell

Reputation: 212434

You really ought to use a function:

sc() { docker container stop $(docker container ls -a | awk 'NR>1 {print $1}'); }

But you could also simplify the construction of the alias by getting rid of the awk:

alias sc='docker container stop $(docker container ls -a -q)'

Upvotes: 2

Related Questions