Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a variable for color. I use it to set color to strings, by evaluating it inside the string. However, I need to include space after the name(so that the name doesn't contain part of the text). This sometimes looks bad.

How can I avoid using(printing) this space?

Example(Let's say that Red=1 and NC=2):

echo -e "$Red Note: blabla$NC".

Output:

1 Note: blabla2.

Expected output:

1Note: blabla2.
share|improve this question
up vote 11 down vote accepted

Just enclose variable in braces:

echo -e "${Red}Note: blabla${NC}".

See more detail about Parameter Expansion.

See also great answer Why printf is better than echo? if you care about portability.

share|improve this answer

What you should get into the habit of using is printf:

printf '%sNote: blabla%s\n' "$Red" "$NC"

Where each %s replace a variable. And the -e is not needed as printf accepts backslash values \n,\t, etc in a portable manner.

Remember that echo -e is not portable to other shells.

Other options:

You could use two commands that concatenate their output:

echo -ne "$Red"; echo -e "Note: blabla$NC"

You could use simple string concatenation:

echo -e "$Red""Note: blabla$NC"

Or you could make use of { } to avoid name-text confusions:

echo -e "${Red}Note: blabla$NC"
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.