-1

First its not like this question How do I echo a string with multiple spaces in bash “untouched”? [duplicate] because in that question he just want to print it and I want to assign it to variable and save it. I've tried this:

SPACE='  '
VAR="$VAR1${SPACE}$VAR2"
1
  • It is unclear what the issue is. The original question lacked a closing double quote. This was corrected after the accepted answer was posted, and the current version never explicitly states the actual issue. Commented Jan 28 at 7:02

3 Answers 3

6

You're just missing the closing double quote:

$ var1=Hello
$ SPACE='  '
$ VAR2=Wissam
$ VAR="$var1${SPACE}$VAR2"
$ echo "${VAR}"
Hello  Wissam

Note that variable names are case-sensitive too.

2

Also since I don't entirely agree with the first answer, here is how I'd do it

var1="Hello"
spaces=10  # a dynamic value
var2="Wissam"
printf -v var3 "%s%$((${#var1} + spaces))s" "$var1" "$var2"
printf "%s\n" "$var3"
1
  • You want ${#var2}. Or instead right-pad the first specifier "%-$((${#var1}+spaces))s%s" Commented Jan 29 at 2:07
1

You can also do it this way:

$ v1="abc def"
$ v2="   "
$ v3="ghi jkl"
$ v4="$v1""$v2""$v3"
$ echo "$v4"
abc def   ghi jkl

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.