Bash不能从函数中打印换行符吗?(Bash not printing newlines from function?)

我有一个我想用来获取/检查网站证书的功能。 我使用ZSH但是想在BASH中测试它以验证它在shell中的效果。

该函数似乎将内容打印为单行,并且没有注意到或保留openssl命令中的换行符。 ZSH处理得很好并按预期工作。

当openssl命令返回时,我该怎么做才能让bash注意到新行?

function get-cert() { if (( $# == 2 )); then site_url=${1} site_port=${2} elif (( $# == 1 )); then site_url=${1} site_port=443 else echo -n "Enter site domain to get and view the cert: " read site_url echo -n "Enter the port [443]: " read site_port site_port=${site_port:-443} fi echo " === Getting cert for site ${site_url}:${site_port}" content="$(openssl s_client -showcerts -servername ${site_url} -connect ${site_url}:${site_port} </dev/null)" if [[ $? == "0" ]]; then echo ${content} else echo "Failed to get cert for ${site_url}" fi }

I have a function I wanted to use for getting/checking website certs. I use ZSH but wanted to test it in BASH as well to verify that it works as well in that shell.

The function seems to print out the content as a single line and isn't noticing or keeping the newlines from the openssl command. ZSH handles it just fine and works as expected.

What can I do to get bash to notice the new lines when the openssl command returns?

function get-cert() { if (( $# == 2 )); then site_url=${1} site_port=${2} elif (( $# == 1 )); then site_url=${1} site_port=443 else echo -n "Enter site domain to get and view the cert: " read site_url echo -n "Enter the port [443]: " read site_port site_port=${site_port:-443} fi echo " === Getting cert for site ${site_url}:${site_port}" content="$(openssl s_client -showcerts -servername ${site_url} -connect ${site_url}:${site_port} </dev/null)" if [[ $? == "0" ]]; then echo ${content} else echo "Failed to get cert for ${site_url}" fi }

最满意答案

echo ${content}

该行将内容分解为单词,并将每个单词作为参数传递给echo,后者用空格分隔它们。 用引号括起来。

echo "${content}" echo ${content}

This line breaks content into words and passes each word as an argument to echo, which separates them with spaces. Wrap it in quotes.

echo "${content}"

更多推荐