22

When invoking long, switch-heavy commands, it's a good practice to write them in shell scripts. Is there an easy way to comment lines in such scripts? I've tried the following methods, but neither works.

# the \ is also commented out, resulting in "command" and "--good-switch".
command \
  #--bad-switch \
  --good-switch \

# seems to send an extra argument to the command
command \
  \ #--bad-switch \
  --good-switch
  • 1
    The second one causes a space to be sent as an argument (\ escapes the next character, which "hides" newlines but makes spaces significant). – geekosaur Mar 21 '11 at 15:27

7 Answers7

18

This might be an option: store the command and args in an array, then execute it after

# build the command
cmd=( ls
        -F
      # -a   # comment out this option temporarily
        -l
    )
# $cmd is now an array with 3 elements

# execute it
"${cmd[@]}"
glenn jackman
  • 85,964
  • This is bash-only… – Socob Sep 28 '16 at 08:11
  • @Socob, that syntax is from zsh and is also supported by ksh93, bash, yash and mksh. – Stéphane Chazelas Mar 11 '24 at 19:42
  • @StéphaneChazelas Yes, sorry, that was slightly imprecise. What I meant was that it’s not POSIX shell-compatible (and the question is tagged [tag:shell]), and limited to bash or (to some degree) “bash-compatible” shells. – Socob Mar 12 '24 at 01:25
4

I always moved the commented ones just after the command.

command \
  --good-switch
# --bad-switch          with explanation here, if needed
geekosaur
  • 32,047
2

See the answer by Digital Ross.

See also the question I just posted, bash multi line command with comments after the continuation character.

This would be a useful feature. It is a pity it does not have standard support.

Faheem Mitha
  • 35,108
2

If you don't mind losing the value of the positional parameters, you can use them to build the command gradually. This also lets you use conditionals.

set -- command --first-option
# We need a special option on Tuesdays
if [ "$(date +%u)" = 2 ]; then
  set -- "$@" --tuesday
fi
set -- "$@" --last-option
# Finally we execute the command
"$@"

If you want to add options before the positional parameters, you might need to build the command backwards.

# We have a list of files in the positional parameters.
set -- --last-option "$@"
# We need a special option on Tuesdays
if [ "$(date +%u)" = 2 ]; then
  set -- --tuesday "$@"
fi
set -- command --first-option "$@"
# Finally we execute the command
"$@"

If you need to preserve the positional parameters, put this code in a function: the positional parameters are restored when the function returns.

muru
  • 72,889
1

a comment brakes the chain of conected lines, so the simplest solution would be to move the commented lines at the end of the list.

command \
  --good-switch \
  # --bad-switch \

If you don't want to change the order, you can use the : command (the colon utility, which does nothing) to keep the connected lines intact:

command \
  `: --bad-switch `\
  --good-switch \

I tested with this:

function command { 
  echo "num args:" $#;
}
rubo77
  • 28,966
1

The issue is that the slashes are removed before the line is parsed, so the first command is parsed as if you'd written command #--bad-switch --good-switch. If you have a really long sequence of commands, you could for example write a line-by-line comment block above or below it, explaining each in turn, or you could store the parameters in a variable (although that often gives quoting headaches with special characters).

l0b0
  • 51,350
  • 1
    That doesn't seem to be the case. --good-switch is interpreted as a command. –  Mar 21 '11 at 15:47
0
$ echo abc `# comment` \
>      def `# comment 2` \
>      ghi
abc def ghi

$ echo abc `# comment` \
>      `#def # comment entire line` \
>      ghi
abc ghi

The backticks perform Command Substituion. This works because there is no standard output from executing a comment.

Peter Tseng
  • 141
  • 4
  • This spaws a new shell for every comment. What a waste of cycles for something basic as a comment. Too bad there is no simple built-in way. – oligofren Feb 14 '22 at 13:36