Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 \
share|improve this question
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
up vote 12 down vote accepted

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

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

# execute it
share|improve this answer

I always moved the commented ones just after the command.

command \
# --bad-switch          with explanation here, if needed
share|improve this answer

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).

share|improve this answer
That doesn't seem to be the case. --good-switch is interpreted as a command. – user4518 Mar 21 '11 at 15:47

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.

share|improve this answer

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:" $#;
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.