8

I like to use the following format in scripts for commands with a lot of parameters (for readability):

docker run \
 --rm \
 -u root \
 -p 8080:8080 \
 -v jenkins-data:/var/jenkins_home \
 -v /var/run/docker.sock:/var/run/docker.sock \
 -v "$HOME":/home \
 jenkinsci/blueocean

But, sometimes I'd like to comment one of these parameters out like:

# -p 8080:8080  

This doesn't work, as the EOL is interpreted as return and the command fails. Tried this too:

\ # -p 8080:8080

which also didn't work.

Question: Is there a way to comment out the parameter, so it's still on its own line, but I'd be able to execute the script?

9

You could substitute an empty command substitution:

docker run \
 --rm \
 -u root \
 $(: -p 8080:8080 ) \
 -v jenkins-data:/var/jenkins_home \
 -v /var/run/docker.sock:/var/run/docker.sock \
 -v "$HOME":/home \
 jenkinsci/blueocean
  • 1
    Not very readable (and Vim doesn't recognise : as the empty command or a comment) but works. – tricasse Feb 6 at 18:07
  • I like that with this solution I can still copy it with the mouse, paste it and run it. Now just need a vi macro to be able to do/undo this change easy, thanks! – Chirlo Feb 6 at 18:36
  • 1
    @tricasse an alternative to : would be true – Jeff Schaller Feb 6 at 20:27
12

You can't comment out a piece of a line.

Note that since the newlines are escaped, the command is actually a single line (to the shell parser), and there's no way to comment out a part of a single line (except for at the very end).

Instead, maybe just make a copy of the original command in an editor and comment it out completely while keeping the modified command uncommented:

docker run \
 --rm \
 -u root \
 -v jenkins-data:/var/jenkins_home \
 -v /var/run/docker.sock:/var/run/docker.sock \
 -v "$HOME":/home \
 jenkinsci/blueocean

# Was originally:
# docker run \
# --rm \
# -u root \
# -p 8080:8080 \
# -v jenkins-data:/var/jenkins_home \
# -v /var/run/docker.sock:/var/run/docker.sock \
# -v "$HOME":/home \
# jenkinsci/blueocean

Alternatively, if you want to occasionally delete or change the -p option and its argument (assuming bash or a shell with the same array syntax):

port=( -p 8080:8080 )

docker run \
 --rm \
 -u root \
 "${port[@]}" \
 -v jenkins-data:/var/jenkins_home \
 -v /var/run/docker.sock:/var/run/docker.sock \
 -v "$HOME":/home \
 jenkinsci/blueocean

Then just change or comment out the assignment to port.

Taking this further:

docker_run_args=(
    --rm 
    -u root 
    -p 8080:8080 
    -v jenkins-data:/var/jenkins_home 
    -v /var/run/docker.sock:/var/run/docker.sock 
    -v "$HOME":/home 
    jenkinsci/blueocean
)

docker run "${docker_run_args[@]}"

Inside the array assignment, there are no issues with commenting out a line:

docker_run_args=(
    --rm 
    -u root 
#    -p 8080:8080 
    -v jenkins-data:/var/jenkins_home 
    -v /var/run/docker.sock:/var/run/docker.sock 
    -v "$HOME":/home 
    jenkinsci/blueocean
)

docker run "${docker_run_args[@]}"
  • 5
    Putting all the arguments in an array is also a great way to do it, since you can have a multi-line array definition with no `\`s and you can comment individual lines... Wanna include that into your answer too? – filbranden Feb 6 at 16:02
  • 1
    @filbranden Will do as it's a good extension of what I already had written. Thanks. – Kusalananda Feb 6 at 16:04

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.