문제

I’d like to embed comments like this

ls -l \
-a \
# comment here
-h \
-t . 

But it seems that’s not possible. Maybe some other variant does exist? Putting a comment right after the backslash or ending a comment with a backslash doesn’t help.

도움이 되었습니까?

해결책 2

As far as I know, it is not possible, for Bash tries to follow to POSIX guidelines and other standards. Especially:

If a <newline> follows the backslash, the shell shall interpret this as line continuation. The backslash and <newline>s shall be removed before splitting the input into tokens. Since the escaped <newline> is removed entirely from the input and is not replaced by any white space, it cannot serve as a token separator.
Shell Command Language, 2.2.1 Escape Character (Backslash)

Thus, it is not possible because of how the shell parser works:

  • A line starting with # is a comment, and nothing after gets evaluated (including a trailing \)
  • A \ that is not at the end of a line, is not a line skip.

In your script:

ls -l \
-a \
# comment here
-h \
-t .

The comment line is replaced with nothing (i.e. it has no token); and since the command, so far, is valid, and as a newline is met (… -a ↵), the shell runs the ls -l -a command, then the -h -t . command (and does not find a -h binary in your $PATH, so it stops right there.)

다른 팁

The posts above do not have a direct solution. However, there is a direct solution that's actually mentioned in even older posts: How to put a line comment for a multi-line command and Commenting in a Bash script.

The solution I like best is:

ls -l `# long format` \
-a `# all files` \
-h `# human readable` \
-t `# time sort`

You will need both the accent grave (`) quotation and the octothorpe (#) to indicate the comment. Use them before the backslash.

You can do something like this with an array, which doesn't require continuation characters:

ls_cmd=(
  ls
  -l  # long form
  -a  # show hidden files
  -h  # human-readable sizes
  -t  # sort by time
  .
)

"${ls_cmd[@]}" # run the command from the array

...but for the general case, the answer is no.

Not directly, buy you can put such comments in an array assignment. (This also allows you to split the arguments across multiple lines without using line continuation.)

cmd_options=(
  -l
  -a
   # comment here
  -h
  -t .
)
ls "${cmd_options[@]}" 

In that situation I usually go with something like

ls -l  -a  -h  -t .  
#  |   |   |   time sort
#  |   |   human readable
#  |   all files
#  long format

or I use the long form of the argument when they exist e.g. --human-readable rather than -h.

...and it can be done after pipes:

echo "Hello beautiful" | \
# Display match only:
grep -o beautiful

But, otherwise... no.

I don't think you can do this directly, because the comment removal is done before than the back-slash line concatenation, so this does not work

# commenting \
seveal lines

The obvious solution is to use # on every line:

# commenting
# seveal lines

If you want to be creative you can use a dirty trick:

: 'This is almost
a multiline comment'

Not actually a comment, but it does nothing, other than changing the exit code to 0.

it's not possible. Because the backslash is a line continuator and the comment a single character which makes everything on its right ignored until end of line:

ls \
# -l \
-h

the -l will get ignored, the ls command executed and the -h considered as a new command.

Because in C-like languages you have comments with boundaries: /* */ it's possible to place those between token in expression that uses line continuation.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top