Bash 长字符串,没有分词?

fer*_*raz 6 command-line shell bash zsh shell-script

我想用长命令整理一些脚本,例如:

{ somecheck || somecomand "reallyreallyreallyreallyreallyreallylongstring" } &> /dev/null &
Run Code Online (Sandbox Code Playgroud)

变成这样:

{ somecheck ||                   \
    somecomand "reallyreally"    \
        "reallyreally"           \
        "reallyreally"           \
        "longstring"             \
} &> /dev/null &
Run Code Online (Sandbox Code Playgroud)

但我担心分词。为了避免这种情况,我正在考虑:

{ somecheck ||                   \
    somecomand  "$(echo          \
        "reallyreally"           \
        "reallyreally"           \
        "reallyreally"           \
        "longstring"             \
    )"                           \
} &> /dev/null &
Run Code Online (Sandbox Code Playgroud)

有没有人知道在 bash/zsh 中处理多行字符串的其他方法?我在谷歌搜索这个信息时遇到了麻烦,我认为这意味着至少三个进程(脚本、后台块和命令替换子shell);也许有更好的方法?

提前致谢!

gle*_*man 6

使用这样的行延续会在您的字符串中添加空格:序列backslash-newline-whitespace将被单个空格替换。

仅使用变量将大大提高可读性:

url="reallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallyreallylongstring"
{ somecheck || somecomand "$url" } &> /dev/null &
Run Code Online (Sandbox Code Playgroud)

您仍然可以使用数组将其分解为子字符串

parts=(
    "reallyreallyreallyreally"
    "reallyreallyreallyreally"
    "reallyreallyreallyreally"
    "reallyreallyreallyreally"
    "longstring"
)
whole=$(IFS=; echo "${parts[*]}")
Run Code Online (Sandbox Code Playgroud)

但鉴于增加的复杂性,拆分文字字符串有那么重要吗?