Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

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

This question already has an answer here:

I assigned a var like this:

MYCUSTOMTAB='     '

But using it in echo both:

echo $MYCUSTOMTAB"blah blah"

or

echo -e $MYCUSTOMTAB"blah blah"

just return a single space and the rest of the string:

 blah blah

How can I print the full string untouched without having it being randomly raped by the echo command? I want to use it for have a custom indent because \t is too much wide for my tastes.

share|improve this question

marked as duplicate by Gilles bash Apr 1 at 15:03

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
Put the variable inside double quotes. This is one of the main reasons we constantly harp "quote your variables". Security implications of forgetting to quote a variable in bash/POSIX shells – glenn jackman Apr 1 at 14:44
up vote 4 down vote accepted

Put your variable inside double quote to prevent field splitting, which ate your spaces:

$ MYCUSTOMTAB='     '
$ echo "${MYCUSTOMTAB}blah blah"
     blah blah
share|improve this answer

I think you just have to use double quotes for your variable

echo -e "$MYCUSTOMTAB"."blah blah"
share|improve this answer

As suggested in this answer quoting the variable is enough.

The reason why quoting is needed in your case is because without it bash replaces the variable in echo -e $MYCUSTOMTAB"blah blah" with its value and effectively executes:

echo -e      "blah blah"

You can also use printf instead of echo:

printf "$MYCUSTOMTAB"

printf "${MYCUSTOMTAB}blah blah\n"

For reference read Why is printf better than echo?

share|improve this answer
2  
While strictly true, the cause here is totally unrelated to the difference between printf and echo. While printf is certainly more portable and has some upsides, its major downside is that it is ugly and unreadable, and way more difficult to understand. – Wouter Verhelst Apr 1 at 14:50
2  
printf is "better" if you use it properly: you need to write printf "blah%sblah\n" "$MYCUSTOMTAB" -- if the variable contains any %s, %d, etc, you'll get the wrong output otherwise. – glenn jackman Apr 1 at 14:51

I know your question is tagged bash but anyway, for maximum portability and reliability, I would use:

printf "%sblah blah\n" "$MYCUSTOMTAB" 

or

someString="blah blah"
printf "%s%s\n" "$MYCUSTOMTAB" "$someString"
share|improve this answer

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