bash 如何指定多行shell变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15429330/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 23:27:12  来源:igfitidea点击:

How to specify a multi-line shell variable?

bashshell

提问by user2219963

I have written a query:

我写了一个查询:

function print_ui_hosts
{
local sql = "select ........."
print_sql "$ sql"
}

local sql - a very long string. Query is not formatted. How can I split a string into multiple lines?

本地 sql - 一个很长的字符串。查询未格式化。如何将字符串拆分为多行?

回答by Nik O'Lai

simply insert new line where necessary

只需在必要时插入新行

sql="
SELECT c1, c2
from Table1, Table2
where ...
"

shell will be looking for the closing quotation mark

shell 将寻找右引号

回答by dogbane

Use readwith a heredoc as shown below:

read与 heredoc 一起使用,如下所示:

read -d '' sql << EOF
select c1, c2 from foo
where c1='something'
EOF

echo "$sql"

回答by islijepcevic

I would like to give one additional answer, while the other ones will suffice in most cases.

我想给出一个额外的答案,而其他答案在大多数情况下就足够了。

I wanted to write a string over multiple lines, but its contents needed to be single-line.

我想在多行上写一个字符串,但它的内容需要是单行的。

sql="                       \
SELECT c1, c2               \
from Table1, ${TABLE2}      \
where ...                   \
"

I am sorry if this if a bit off-topic (I did not need this for SQL). However, this post comes up among the first results when searching for multi-line shell variables and an additional answer seemed appropriate.

如果这有点偏离主题,我很抱歉(我不需要这个用于 SQL)。然而,当搜索多行 shell 变量时,这篇文章出现在第一个结果中,一个额外的答案似乎是合适的。

回答by Brad Parks

Thanks to dimo414's answer to a similar question, this shows how his great solution works, and shows that you can have quotes and variables in the text easily as well:

感谢dimo414 对类似问题的回答,这显示了他出色的解决方案是如何工作的,并表明您也可以轻松地在文本中使用引号和变量:

example output

示例输出

$ ./test.sh

The text from the example function is:
  Welcome dev: Would you "like" to know how many 'files' there are in /tmp?

  There are "      38" files in /tmp, according to the "wc" command

test.sh

测试文件

#!/bin/bash

function text1()
{
  COUNT=$(\ls /tmp | wc -l)
cat <<EOF

   Would you "like" to know how many 'files' there are in /tmp?

  There are "$COUNT" files in /tmp, according to the "wc" command

EOF
}

function main()
{
  OUT=$(text1 "Welcome dev:")
  echo "The text from the example function is: $OUT"
}

main

回答by EndlosSchleife

readdoes not export the variable (which is a good thing most of the time). Here's an alternative which can be exported in one command, can preserve or discard linefeeds, and allows mixing of quoting-styles as needed. Works for bash and zsh.

read不导出变量(大多数时候这是一件好事)。这是一种可以在一个命令中导出的替代方法,可以保留或丢弃换行符,并允许根据需要混合引用样式。适用于 bash 和 zsh。

oneLine=$(printf %s \
    a   \
    " b "   \
    $'\tc\t'    \
    'd '    \
)
multiLine=$(printf '%s\n' \
    a   \
    " b "   \
    $'\tc\t'    \
    'd '    \
)

I admit the need for quoting makes this ugly for SQL, but it answers the (more generally expressed) question in the title.

我承认引用的需要使 SQL 变得丑陋,但它回答了标题中的(更普遍表达的)问题。

I use it like this

我像这样使用它

export LS_COLORS=$(printf %s    \
    ':*rc=36:*.ini=36:*.inf=36:*.cfg=36:*~=33:*.bak=33:*$=33'   \
    ...
    ':bd=40;33;1:cd=40;33;1:or=1;31:mi=31:ex=00')

in a file sourced from both my .bashrcand .zshrc.

在来自 my.bashrc.zshrc.