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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With