2

Knowing I am able to run echo or mv to expand patterns like these: echo {0..9}{A..Z}. I am curious to know if there is a way to do the same but to run commands?

docker-compose {stop,rm,up -d}

The example above does not work, but there is some way to accomplish that (to run stop, rm and up separately)?

fedorqui
  • 275,237
  • 103
  • 548
  • 598
Jean Carlo Machado
  • 1,480
  • 1
  • 15
  • 25

2 Answers2

5

Not the way you mention it.

Brace expansion occurs before any other expansion. This means that when you say echo {0,1}{a,b}, Bash expands the braces before going through any other step. This way, it becomes echo 0a 0b 1a 1b, a single command.

When you mention docker-compose {stop,rm,up -d}, note this would expand to a single command: docker-compose stop rm up -d, which doesn't seem to be valid.

It looks like you would like to run three different commands:

docker-compose stop
docker-compose rm
docker-compose up -d

For this, you may want to use a loop (note "up -d" is quoted so that it is treated as a single argument):

for argument in stop rm "up -d"
do
    docker-compose $argument
done
fedorqui
  • 275,237
  • 103
  • 548
  • 598
  • 1
    Note that quoting is only necessary for `"up -d"` otherwise it would be two words. – Karoly Horvath Nov 16 '15 at 10:54
  • 2
    Also note that `"$parameter"` is almost certainly *wrong* as the docker will receive a single argument `up -d`... Just get rid of the quotes. – Karoly Horvath Nov 16 '15 at 10:57
  • @KarolyHorvath not sure if there is a difference between `docker-compose "up -d"` and `docker compose up -d`. – fedorqui Nov 16 '15 at 11:01
  • 1
    There's a huge difference, one receives just 1 argument the other 2. Anything that is using standard argument parsing techniques (http://stackoverflow.com/questions/192249/how-do-i-parse-command-line-arguments-in-bash so pretty much anything) will fail on `"up -d"`. – Karoly Horvath Nov 16 '15 at 11:08
2

You can use eval:

eval docker-compose\ {stop,rm,'up -d'}\;

Careful escaping/quoting is needed, though. Just test with echo instead of docker that it emits the correct commands:

$ echo docker-compose\ {stop,rm,'up -d'}\;
docker-compose stop; docker-compose rm; docker-compose up -d;
choroba
  • 231,213
  • 25
  • 204
  • 289