Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i comment command line arguments on multiple lines?

Tags:

bash

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 comment tho, so -flag arg is treated as a new command.

like image 541
AJcodez Avatar asked Sep 05 '25 11:09

AJcodez


1 Answers

mattyice has a good suggestion.

An alternative approach is to stick the command in an array to reduce the noise overhead of commenting. This also allows printing the command in such a way that it can easily be reissued outside of 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 redirections would go on the execution line, not in the array.

like image 120
that other guy Avatar answered Sep 11 '25 09:09

that other guy