What is $()
In a shell script or terminal command, the $()
syntax is known as “command substitution.” It allows you to execute a command and use its output as part of another command or assigned to a variable. In the context of $(docker ps -q)
, it executes the docker ps -q
command and captures its output (the container IDs of running containers in this case). Therefore we can combine shell commands.
For example, if docker ps -q
returns two container IDs: 1234abcd
and 5678efgh
.
Using docker stop $(docker ps -q)
is equivalent to running docker stop 1234abcd 5678efgh
. The command substitution allows you to dynamically pass the output of one command as an argument to another command.