How do we separate variables from letters in shell scripting?

Just surround the variable's name with curly braces.

#!/bin/bash

ANIMAL="Dog"
echo "${ANIMAL}s are the best."
exit 

#!/bin/bash


ANIMAL="Dog"
echo "${ANIMAL}s are the best."
exit 

The answer is no longer unique, but correct...


With braces: echo "${ANIMAL}s are the best."

With quotes: echo "$ANIMAL"'s are the best.'

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

I wouldn't use the quotes one most of the time. I don't find it readable, but it's good to be aware of.

Tags:

Bash