Catfish
Catfish

Reputation: 19294

How to use a variable without a space before the next word?

I have a simple bash script that looks like this:

RED=$(tput setaf 1)
echo "$REDERROR - ..."

and I want it to print ERROR in red.

If i change my code to this:

RED=$(tput setaf 1)
echo "$RED ERROR - ..."

it prints ERROR in red but with a leading space.

So how can I eliminate that leading space and still reference my variable $RED before it?

Upvotes: 1

Views: 121

Answers (2)

Petr Skocik
Petr Skocik

Reputation: 60107

Use curly braces:

echo "${RED}ERROR - ..."

String concatenation also works:

echo "$RED""ERROR - ..."

(Quotes around $RED aren't technically needed given its particular contents (no spaces or other field seperators) and therefore echo $RED"ERROR - ..." will give the same result.)

Upvotes: 5

JuniorCompressor
JuniorCompressor

Reputation: 20025

You can use:

 echo $RED"ERROR - ..."

Upvotes: 1

Related Questions