Bash long string, no word splitting?
Solution 1
Using line continuations like that adds spaces into your string: the sequence backslash-newline-whitespace
will be replaced by a single space.
Just using a variables will go a long way towards improved readability:
url="reallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallylongstring"
{ somecheck || somecomand "$url" } &> /dev/null &
You can still break that up into substrings, using an array
parts=(
"reallyreallyreallyreally"
"reallyreallyreallyreally"
"reallyreallyreallyreally"
"reallyreallyreallyreally"
"longstring"
)
whole=$(IFS=; echo "${parts[*]}")
but is it that important to split up literal strings given the added complexity?
Solution 2
Well, you could simplify it slightly to:
$ [ -f /dev/null ] || sed 's/a/A/g' <(echo "thisis""avery"\
"long""string"\
"Ihave""separated"\
"into""smaller"\
"substrings")
thisisAverylongstringIhAvesepArAtedintosmAllersubstrings
The important point is to not have any extra whitespace in the input string, so no spaces around the \
.
Glenn's suggestion to use variables is both prettier and simpler though, go with that.
Related videos on Youtube
ferhtgoldaraz
Updated on September 18, 2022Comments
-
ferhtgoldaraz over 1 year
I want to tidy up some script with long commands, so that for example:
{ somecheck || somecomand "reallyreallyreallyreallyreallyreallylongstring" } &> /dev/null &
becomes something like:
{ somecheck || \ somecomand "reallyreally" \ "reallyreally" \ "reallyreally" \ "longstring" \ } &> /dev/null &
But I'm worried about word splitting. To avoid it, I'm considering this:
{ somecheck || \ somecomand "$(echo \ "reallyreally" \ "reallyreally" \ "reallyreally" \ "longstring" \ )" \ } &> /dev/null &
Does any one know some other way to do multiline strings in bash/zsh? I'm having trouble googling for this info, and I think this'll mean at least three processes (the script, the background block, and the command substitution subshell); Maybe there's a better way?
Thanks in advance!
-
terdon about 9 yearsJust to clarify, you don't actually want multiline strings right? The whole issue is to not include the newlines in the string but use them only for readability, is that correct?
-
ferhtgoldaraz about 9 yearsYes, the string could be for example an URL
-
Overmind Jiang about 9 yearsHow long is "really long"? At work, we have a program where the command line (which is mostly machine-generated) is encroaching on a 128 KiB limit, and of the nearly 200 arguments, one is over 100 KiB. That's extremely long by any reasonable definition. It's hard to copy'n'paste that for analysis.
-
ferhtgoldaraz about 9 yearsArbitrarily long, though my use cases are not that extreme
-
-
Angel Todorov about 9 yearsMake sure you quote your
"$vars"
unless you want word splitting and filename generation on the values. -
Angel Todorov about 9 yearsyes
+=
works with bash for appending strings.