By Chirlo


2019-02-06 15:46:01 8 Comments

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?

2 comments

@Kusalananda 2019-02-06 15:57:01

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[@]}"

@filbranden 2019-02-06 16:02:37

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?

@Kusalananda 2019-02-06 16:04:07

@filbranden Will do as it's a good extension of what I already had written. Thanks.

@Jeff Schaller 2019-02-06 16:01:02

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

@tricasse 2019-02-06 18:07:05

Not very readable (and Vim doesn't recognise : as the empty command or a comment) but works.

@Chirlo 2019-02-06 18:36:22

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!

@Jeff Schaller 2019-02-06 20:27:13

@tricasse an alternative to : would be true

Related Questions

Sponsored Content

16 Answered Questions

[SOLVED] "Bookmarks" for bash

  • 2011-12-14 05:00:26
  • Casebash
  • 6100 View
  • 24 Score
  • 16 Answer
  • Tags:   bash shell

3 Answered Questions

[SOLVED] Iterate through multiline string line by line

  • 2019-03-31 10:52:14
  • Steiner
  • 140 View
  • 2 Score
  • 3 Answer
  • Tags:   shell

3 Answered Questions

[SOLVED] bash multi line command with comments after the continuation character

  • 2011-08-21 15:14:21
  • Faheem Mitha
  • 20919 View
  • 27 Score
  • 3 Answer
  • Tags:   bash shell

1 Answered Questions

[SOLVED] How to rewrite multiline path into one-line relative path

  • 2015-10-19 17:27:04
  • Son Goku ssj4
  • 90 View
  • 2 Score
  • 1 Answer
  • Tags:   shell-script

2 Answered Questions

[SOLVED] Parsing C Style Multiline Comment

3 Answered Questions

[SOLVED] cshrc execute bashrc within itself?

  • 2011-03-24 14:23:27
  • Jon Phenow
  • 4761 View
  • 2 Score
  • 3 Answer
  • Tags:   shell bash fedora csh

1 Answered Questions

[SOLVED] Comment multiple line using SED

  • 2014-04-23 02:05:31
  • zieggy
  • 1299 View
  • -2 Score
  • 1 Answer
  • Tags:   sed scripting

2 Answered Questions

Sponsored Content