1

When I have a long line of calling a command with its arguments, is it possible to break it into several lines, and have a commend for each line?

For example, if I break free -h into two lines

free \
-h

how can I add a comment for each line? An attempt like this one

free \ # this is the command
-h  # this is the argument

doesn't work.

Tim
  • 1
  • 141
  • 372
  • 590
  • 1
    [bash-multi-line-command-with-comments-after-the-continuation-character](http://unix.stackexchange.com/questions/19124/bash-multi-line-command-with-comments-after-the-continuation-character) – Jose Ricardo Bustos M. Oct 28 '15 at 19:28
  • 2
    Not quite a perfect duplicate, so I won't wield my hammer but I think that basically the only way you can do this is using one of the techniques mentioned here: http://stackoverflow.com/q/2524367/2088135 – Tom Fenech Oct 28 '15 at 19:28
  • solution [by DigitalRoss](http://stackoverflow.com/a/1456019/4767343) – Jose Ricardo Bustos M. Oct 28 '15 at 19:31
  • found a better one :) – Tom Fenech Oct 28 '15 at 19:31
  • I'm surprised none of those duplicates show a way to do that without forking a number of subshells. (oh, Jose's link does.) – chepner Oct 28 '15 at 19:33
  • Alas, this question was closed before I could answer. The answer was provided in the comment section of the linked-to article, but not as a proper answer. I'll add it here even though formatting is non-existent in the comment section. This is on two lines. While a little clunky it works without spawning subshells: `who ${IFS# This is the command} \ -u ${IFS# This is the argument}` – Gary_W Oct 28 '15 at 20:05
  • @Gary_W That assumes `IFS` contains only whitespace (which is generally a good assumption, but not guaranteed). – chepner Oct 28 '15 at 20:18
  • Although you could define a variable yourself to use instead of `IFS`. `comment=; who ${comment# This is the command \ ...`. I'd post that to the linked duplicate, which isn't closed. – chepner Oct 28 '15 at 20:27
  • True I should have said an alternative answer. – Gary_W Oct 28 '15 at 20:27
  • @Chepner that is the best method yet in my opinion. Please post that to the linked post so I can vote for it and call it a day. – Gary_W Oct 28 '15 at 20:30

2 Answers2

3

Store the arguments in an array, and document them there.

args=(--arg1      # First option
      --arg2 bar  # Second option with an argument
)

# Run free with the above arguments
free "${args[@]}"
chepner
  • 497,756
  • 71
  • 530
  • 681
2

A classic trick for this is using inlined comments in backticks:

free `#This is the command` \
-h   `#This is the argument`
that other guy
  • 116,971
  • 11
  • 170
  • 194