April 22, 2017

Relevant Linux Features: Command Substitution

Command substitution - $(command)

In command substitution, the shell runs command, however instead of displaying the output of command, it stores the output in a variable. You can then pass that variable as input to another command.

The syntax of command substitution is $(command) or the older `command`, using back-ticks.

Let's say you want to remove the most recent container running. You can use docker ps -a which lists all containers, starting with the most recent, then copy the Container ID into the docker rm <Container ID> command:

Alternatively, you can use Command Substitution and let the system do some of the work for you. The following command runs docker ps -lq which gets the ID of the most recent container, it passes that ID to the docker rm command: $ docker rm $(docker ps -lq):

No comments:

Post a Comment