我的脚本有一个奇怪的问题,我想知道它是否存在解决问题的方法。
我把它放在一个脚本中供你测试:
#!/bin/bash
# Clear the screen
clear
# Define the array
ARRAY=("\e[2;3HLine one" "\e[3;3HLine two" "\e[4;3HLine three")
# Change background color to "teal" and foreground color to "black"
echo -e "\e[0;30m\e[46m"
# Print each line one by one on the screen
# This works as expected, nothing to say
echo -e "${ARRAY[0]}"
echo -e "${ARRAY[1]}"
echo -e "${ARRAY[2]}"
# The exact same text, just moved the text below the previous one to make it easier to compare
ARRAY=("\e[6;3HLine one" "\e[7;3HLine two" "\e[8;3HLine three")
# Print the entire array in one shot, to gain speed
# This does not work as expected as there is a "space" at the end of the 2 first lines
echo -e "${ARRAY[@]}"
# Reset the colors to normal
echo -e "\e[0m"
启动时,您可以在第二次打印中看到第一行和第二行末尾有一个“空格”。
问题是:如何摆脱这些空间?!
注意:我需要这种格式化,我需要立即打印整个数组......顺便说一句,我已经玩过IFS和字符替换,但到目前为止没有按预期工作......
编辑: Cyrus在下面使用“ printf ”而不是“ echo ”提出的解决方案似乎按预期工作(我'我不确定使用它而不是“echo”内置的利弊,但现在它将完成这项工作。
解决方案:
printf "%b\n" "${ARRAY[@]}"
而不是:
echo -e "${ARRAY[@]}"
再次感谢赛勒斯的帮助。
答案 0 :(得分:2)
替换
echo -e "${ARRAY[@]}"
通过
printf "%b\n" "${ARRAY[@]}"