How can I comment out command line arguments on multiple lines?

I want to write a bash script with nice comments on all arguments.

#!/bin/bash

command \
  # this is important because ..
  -flag arg \
  # this is also important b/c ..
  --other-option \
  # etc..

      

The backslash only escapes the newline before the tho comment, so it is -flag arg

treated as a new command.

+3


source to share


2 answers


mattyice has a good suggestion.

An alternative approach is to stick to the command in an array to reduce the noise overhead for comments. It also allows the command to be printed in such a way that it can be easily reissued outside the script:



cmd=(
  tar

  # Extract a named, gzipped file
    xzf "$file"

  # Ignore the leading directory
    --strip-components=1
)

# Optionally print the command in copy-pasteable format
echo "Executing: "
printf "%q " "${cmd[@]}"
echo

# Execute the command:
"${cmd[@]}" 

      

Any piping or redirection will be done on the execution line, not the array.

+3


source


What you are looking for is "inline comments". You can simulate them by backtracking (`) and ending with a slash (\) string like this:

echo \
  `# this is a comment` \
  -e 'duuuuddddee!!!'

      



As noted in the comments, there is some overhead with this solution

+3


source







All Articles