-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"
  • 2
    As a side note, only Environment variables are capitalized. Having bash normal vars capitalized causes confusion most of the time. – Valentin Bajrami Dec 03 '16 at 15:29
  • @val0x00ff [Citation needed] there — how do you explain variables such as http_proxy? – Stephen Kitt Dec 03 '16 at 15:35
  • @StephenKitt I'm talking about variables that are produced calling env . The variable http_proxy is not part of a bash shell. Variables like PATH , HOME , PWD, etc are. – Valentin Bajrami Dec 03 '16 at 15:42
  • @val0x00ff Run env | grep proxy and see what comes out... – Stephen Kitt Dec 03 '16 at 15:46
  • @StephenKitt I'm not sure what third party application you've installed but I don't have a 'proxy' whatsoever environment variable on bash. Also I don't see the relevancy of this at all. Once again By convention, environment variables (PATH, EDITOR, SHELL, ...) and internal shell variables (BASH_VERSION, RANDOM, ...) are fully capitalized. All other variable names should be lowercase. Since variable names are case-sensitive, this convention avoids accidentally overriding environmental and internal variables. – Valentin Bajrami Dec 03 '16 at 15:55
  • 1
    @val0x00ff Once again, [citation needed]. I agree it's a useful convention, but it's just a convention — in the shell, a variable is always a shell variable, and it becomes an environment variable if it's exported (or if it was present in the shell's environment). Case has nothing to do with it. – Stephen Kitt Dec 03 '16 at 16:02
  • @StephenKitt I think my previous comment explains itself quite clearly. Imagine if you wer to override HOME or more importantly you'll use a var called PATH which will point to a directory. This causes not only confusion but poses unexpected results as well. A nice reading about it is here: http://mywiki.wooledge.org/Environment – Valentin Bajrami Dec 03 '16 at 16:09
  • 1
    @val0x00ff All I'm saying is that your initial comment, "only environment variables are capitalized", is incorrect since you can perfectly well have lower-case or mixed-case environment variables. Having a convention to avoid problems is a good thing. I'm still looking for the source of your "By convention, ..." quote (assuming it is a quote). – Stephen Kitt Dec 03 '16 at 16:13
  • @tomas I do not believe that this question applies specifically to bash -- any shell would have these quoting issues. – Jeff Schaller Dec 03 '16 at 21:46
  • @JeffSchaller Fair enough. –  Dec 03 '16 at 21:56

3 Answers3

5

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.

Stephen Kitt
  • 434,908
3

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 "%s%$((${#var1} + spaces))s%s\n" "$var1" "$var2"
2

You can also do it this way:

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