Pattern expansion to run commands

前端 未结 2 681
说谎
说谎 2021-01-22 01:21

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 sam

相关标签:
2条回答
  • 2021-01-22 01:53

    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;
    
    0 讨论(0)
  • 2021-01-22 02:06

    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
    
    0 讨论(0)
提交回复
热议问题