1

I'm using zsh which I understand is different to bash, but mostly similar. I decided to make a macro for my docker-compose command:

dock-comp() {
   state="$2"
   if [ "$state" = "up" ]; then
      state='up -d'
   fi

   # For debugging
   echo "docker-compose -p $1 -f my/path/to/file.yml "$(echo "$state")""

   # The actual command that runs
   docker-compose -p $1 -f my/path/to/file.yml "$(echo "$state")"
}

So if I run dock-comp CONTAINER down that works fine, but I get issues with dock-comp CONTAINER up. You can see if I enter up it should append the -d and the echo print confirms that this becomes what I want, but then the command gives the following output:

> dock-comp CONTAINER up            
docker-compose -p CONTAINER -f my/path/to/file.yml up -d
No such command: up _d

Commands:
  build              Build or rebuild services
(etc)

This is the closest post I can find to my issue, but I don't think its that relevant since I'm not using the same arguments thing and my echo proves it should be processed right, but bash is weird so maybe I'm missing something obvious.

1
  • I don't know zsh, but for bash you would try "bash -x your_script and parameters" to see what is executed.
    – U. Windl
    Jan 12 at 9:41

1 Answer 1

2

Because of the quotes, docker-compose sees up -d as a single command, not as up with a -d option. Presumably its parsing then replaces - with _, but it’s too late anyway.

Dropping the quotes and the unnecessary echo and changing state to an array (since it can contain multiple parameters) fixes things:

dock-comp() {
   state=($2)
   if [[ $state[1] == up ]]; then
      state+=(-d)
   fi
   docker-compose -p "$1" -f my/path/to/file.yml $state
}
1
  • I see, I assumed when the command ran it would re-parse the string, but now that I'm thinking about it, that's no a good idea since there are cases where you want to preserve the whitespace in the strings. And yeah the echo was from a leftover debugging shot.
    – Protofall
    Jan 13 at 5:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.