-2

Here is what I am trying to do:

    docker exec -it $1 /bin/bash <<< $CONTAINER_ID

But this doesn't work:

Output: "docker exec" requires at least 2 arguments.

It seems that <<< completely replaces STDIN ignoring $1. Is it possible to replace only one argument with <<<? I know about aliases, but it doesn't suit me. I need to be able to use only standard tools to pass to my colleague a command in which he will change only the last variable, which will replace one or more arguments in the center of the command.

2 Answers2

0

<<< is the wrong tool for this job. It replaces standard input (stdin), which has nothing to do with passing arguments. Arguments and stdin are both forms of input to a command, but they're completely separate from each other. xargs can convert stdin to arguments, but at that point you're really trying to work around the problem instead of using a method that doesn't have this problem in the first place.

I'm not sure I understand what the actual goal and constraints here, but I think defining a function might be the cleanest way to accomplish what you want:

execit() {
    docker exec -it "$1" /bin/bash
}

(Replacing execit with whatever function name you prefer.) Then just run e.g. execit "$CONTAINER_ID".

If that doesn't fit what you're trying to do, the next cleanest solution is what Kamil Maciorowski suggested in a comment:

sh -c 'exec docker exec -it "$1" /bin/bash' sh "$CONTAINER_ID"
-2

Thanks to Kamil Maciorowski, I figured out which way to look and found a solution. Here is the command that does what I need:

xargs -o -I{} docker exec -it {} /bin/bash <<< $CONTAINER_ID
  • 2
    What is wrong with docker exec -it "$CONTAINER_ID" /bin/bash? – Kamil Maciorowski May 11 '23 at 09:37
  • @KamilMaciorowski my task was to move the container name argument to the right. This makes it possible to copy a command from an internal work instruction, and then quickly replace the container name on the right with the desired one, without changing anything in the center. – Anton Kuznetsov May 11 '23 at 09:54
  • 3
    @AntonKuznetsov, that's not what your question says, though. – ilkkachu May 11 '23 at 09:54
  • 3
    Ditto. And frankly only after you published your answer I understood what you wanted the command to do. – Kamil Maciorowski May 11 '23 at 09:57
  • A oneline function is probably still better even for this: f() { docker exec -it "$1" /bin/bash ; }; f "$CONTAINER_ID" – muru May 12 '23 at 09:33