Skip to content

Instantly share code, notes, and snippets.

@mrclay
Last active August 1, 2025 03:15
Show Gist options
  • Save mrclay/36149a3a0d068ca2a2dc8b784a262135 to your computer and use it in GitHub Desktop.
Save mrclay/36149a3a0d068ca2a2dc8b784a262135 to your computer and use it in GitHub Desktop.
Build up an inline string in bash with clearer whitespace handling
#!/usr/bin/env bash
# Strategy 1: trim whitespace from heredoc:
curl "$(tr -d '[:space:]' <<EOD
http://www.example.com/articles/technology/2024/07/10
/this-is-an-example-of-a-very-long-article-title-with-lots
?query=example&page=2
#section3
EOD
)"
# Strategy 2: build up temp var in subprocess:
# Below "a" is built up in the subprocess and does not
# clobber $a in the current environment.
curl "$( \
a='http://www.example.com/articles/technology/2024/07/10'; \
a+='/this-is-an-example-of-a-very-long-article-title-with-lots'; \
a+='?query=example&page=2'; \
a+='#section3'; \
echo ${a})"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment