是否可以在 shell 脚本的多行命令中包含内联注释?

mik*_*ana 0 bash shell

我知道我可以在 bash 中使用分隔长命令\- 有没有办法编写内联注释?

例如,类似于:

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'
Run Code Online (Sandbox Code Playgroud)

如果可以使用zsh pwsh或类似,我也很感兴趣。

Cha*_*ffy 6

不是直接的,但您可以将参数存储在多行定义的数组中,并且可以在这些行之间添加注释。

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[@]}"
Run Code Online (Sandbox Code Playgroud)