0

I know I can separate long commands in bash using \ - is there a way to write inline comments?

For example, something like:

wget \
  # Needed to get to end of around 100 pages of results
  --level=0 \
  # Save into downloads directory
  --directory=downloads \
  --recursive \
  # Normally wget won't span hosts, and .example.com use a CDN
  --span-hosts --domains='assets.publishing.example.com,www.example.com' \
  # Only care about links matching this regex
  --accept-regex 'assets|swag' --regex-type pcre 
  # The site we actually want to scrape
  'https://www.example.com/swag'

If this is possible using zsh pwsh or similar I'm also interested.

mikemaccana
  • 110,530
  • 99
  • 389
  • 494
  • Please tag for only one shell at a time. – Charles Duffy Feb 17 '21 at 16:34
  • I'd go with bash, and ask a separate question for powershell (it's from a different enough ecosystem that you won't find many people who are deep experts in both POSIX-y shells and Windows-y shells, so a single answer that covers both ideally is unlikely, unless it's synthesizing two answers by different people -- which would mean neither of those people would get the answered-this-question check). – Charles Duffy Feb 17 '21 at 16:37
  • BTW, it looks like this has been asked before for bash at https://stackoverflow.com/questions/9522631/how-to-put-a-line-comment-for-a-multi-line-command – Charles Duffy Feb 17 '21 at 16:40
  • I know bash in depth, and do have _some_ experience with zsh and powershell -- but the zsh experience I had was to the effect of "tried using it as my main shell for six months, discovered that it led me to make mistakes when writing code for other, more strictly POSIX-compliant shells, and abandoned it", 15 years ago. Similarly, I use powershell when I'm doing a Windows CTF exercise, but that doesn't mean I have the depth of understanding that I have for bash (or the POSIX sh specification) for any other shell language. – Charles Duffy Feb 17 '21 at 16:41
  • ...it's like how tagging both `c` and `c++` is also frowned on. They're different languages. A question should target one or the other -- even though there, the overlap is even greater. – Charles Duffy Feb 17 '21 at 16:41
  • Isn't this covered by https://stackoverflow.com/q/9522631/3266847 or https://stackoverflow.com/q/1455988/3266847? I'm hesitant to close, though, because the answer here is nice ;) – Benjamin W. Feb 17 '21 at 16:45
  • (...and that's the other part of why tagging for multiple languages is frowned on: When you ask a question only about bash, it can be closed as a duplicate of another question that was only about bash; when you ask a question about four different languages, it needs a four-question duplicate list... or just to be closed as "too broad", which is the more likely action) – Charles Duffy Feb 17 '21 at 16:45
  • @BenjaminW., eh, go ahead; I'll migrate the answer over to whatever we're closed as a duplicate of. – Charles Duffy Feb 17 '21 at 16:45
  • OK will remove the other languages and close. – mikemaccana Feb 17 '21 at 16:46

2 Answers2

6

Not directly, but you can store your arguments in an array defined over multiple lines, and can have comments between those lines.

wget_args=(
  # Needed to get to end of around 100 pages of results
  --level=0
  # Save into downloads directory
  --directory=downloads
  --recursive
  # Normally wget won't span hosts, and .example.com use a CDN
  --span-hosts --domains='assets.publishing.example.com,www.example.com'
  # Only care about links matching this regex
  --accept-regex 'assets|swag' --regex-type pcre 
  # The site we actually want to scrape
  'https://www.example.com/swag'
)
wget "${wget_args[@]}"
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • Thanks Charles! Need to wait to accept this answer but it's the best so far. – mikemaccana Feb 17 '21 at 16:40
  • FYI: The essentials of this answer are already given in an answer to one of the duplicates, at https://stackoverflow.com/a/9522766/14122. Keeping this around but flagged Community Wiki for reference. – Charles Duffy Feb 17 '21 at 16:54
-1

Yes you can, you just have to add `` around your comments like this :

wget \
  `# Needed to get to end of around 100 pages of results`

  --level=0 \\
  `# Save into downloads directory`

  --directory=downloads \\
  --recursive \
Nooble
  • 11
  • 2
  • Yeah, if you write like that, it won't be a command substitution, but you get a literal backtick instead. And it's not like that's a comment anyway, command substitutions _execute the code_ inside, so if you ever make the mistake of putting anything else in there (e.g. if you forget the `#`)... Also, Bash forks a copy of itself for each and every of those command substitutions, so you waste execution time for your "comments". Just use comments as comments and command substitutions for executing commands and capturing their output. – ilkkachu Feb 17 '21 at 16:52