Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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
share|improve this question
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
add comment

4 Answers 4

up vote 9 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
        -F
      # -a   # comment out this option temporarily
        -l
    )
# $cmd is now an array with 3 elements

# execute it
"${cmd[@]}"
share|improve this answer
add comment

I always moved the commented ones just after the command.

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

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
1  
That doesn't seem to be the case. --good-switch is interpreted as a command. –  Tim N Mar 21 '11 at 15:47
add comment

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
add comment

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.