bash:命令在终端中有效,在脚本中无效

bash: command works in terminal, not in script

我在做

a="$(openssl x509 -in /path/to/pemfile.pem -text -noout)";
echo ${a} |grep -a1 -b2 Signature

如果我将这条线放入终端,效果会很好

但是,如果我将同一行放入文件 executeme.shchmod +x executeme.sh./executeme.sh

我似乎没有在变量中创建换行符,导致 grep 只接收一行。终端输出如下:

20- Version: 3 (0x2)
38- Serial Number: 32 (0x27)
64: Signature Algorithm: md5WithRSAEncryption
107- Issuer: C=EN, ST=a, L=b, O=c, OU=d, CN=e
244- Validity
------
[...]

脚本输出整个证书,好像我只会做 a="$(openssl ...)"; echo ${a}

echo "${a}" |grep -a1 -b2 Signature #mind the double quotes

为什么要用双引号?

参见 [ this ] answer + [ this ] 答案。