我们如何在shell脚本中将变量与字母分开

Joh*_*man 17 bash

我试过打印"狗是最棒的".用这个bash脚本.

#!/bin/bash

ANIMAL="Dog"
echo "$ANIMALs are the best."
exit 
Run Code Online (Sandbox Code Playgroud)

但是,我得到了"最好的".打印出来,因为sin $ANIMALS没有与变量分开.我该如何分开?

koj*_*iro 35

带括号: echo "${ANIMAL}s are the best."

报价: echo "$ANIMAL"'s are the best.'

使用printf: printf '%ss are the best.\n' "$ANIMAL"

我大部分时间都不会使用引号.我觉得它不易读,但要注意这一点很好.

  • 啊,匿名匿名投票者。在StackOverflow上回答问题的剪纸。 (2认同)

phl*_*tos 7

用大括号括起变量的名称.

#!/bin/bash

ANIMAL="Dog"
echo "${ANIMAL}s are the best."
exit 
Run Code Online (Sandbox Code Playgroud)