我无法弄清楚如何缩进部分bash脚本,保留代码中的缩进。我希望输出格式正确,输出行没有任何制表符/空格前缀。
ex:script
#!/bin/bash
INFO1="something
output1"
INFO2="output2"
MY_INFO=INFO1
if [ True ]; then
INFO="
Here are the test results
bbb
ccc
aaa
${!MY_INFO}
"
fi
echo "${INFO}"
输出返回:
Here are the test results
bbb
ccc
aaa
something
output1
预期产出:
Here are the test results
bbb
ccc
aaa
something
output1
答案 0 :(得分:1)
保留空格的行情不是错误,它是一个功能。这是双引号的用途。
另一个问题是bash
,(与python
不同),不知道有关缩进可读性的事情 - bash
一个未引用的空间与一千个相同
各种补救措施:
引用多行字符串时的退保缩进,即:
if [ True ]; then
INFO="
Here are the test results
bbb
ccc
aaa
${!MY_INFO}
"
fi
使用bash
,(或其他工具),使缩进消失。所以首先定义一个缩进的多行字符串:
foo="
bar
baz"
然后调整 $ foo 以删除空格:
foo="${foo// }"
现在, $ foo 不再缩进,但如果有空格应该保留,那就太过分了。
与以前相同,但在展示时间,(这更浪费),即:
echo "${foo// }"