How to assign a string with multiple spaces to a variable in bash?

12,807

Solution 1

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.

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

Solution 3

You can also do it this way:

$ v1="abc def"
$ v2="   "
$ v3="ghi jkl"
$ v4="$v1""$v2""$v3"
$ echo "$v4"
abc def   ghi jkl
Share:
12,807

Related videos on Youtube

Wissam Roujoulah
Author by

Wissam Roujoulah

Updated on September 18, 2022

Comments

  • Wissam Roujoulah
    Wissam Roujoulah almost 2 years

    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"
    
    • Marek Zakrzewski
      Marek Zakrzewski over 7 years
      As a side note, only Environment variables are capitalized. Having bash normal vars capitalized causes confusion most of the time.
    • Marek Zakrzewski
      Marek Zakrzewski over 7 years
      @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.
    • Stephen Kitt
      Stephen Kitt over 7 years
      @val0x00ff Run env | grep proxy and see what comes out...
    • Marek Zakrzewski
      Marek Zakrzewski over 7 years
      @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.
    • Stephen Kitt
      Stephen Kitt over 7 years
      @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.
    • Marek Zakrzewski
      Marek Zakrzewski over 7 years
      @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: mywiki.wooledge.org/Environment
    • Stephen Kitt
      Stephen Kitt over 7 years
      @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).
    • Jeff Schaller
      Jeff Schaller over 7 years
      @tomas I do not believe that this question applies specifically to bash -- any shell would have these quoting issues.
    • Admin
      Admin over 7 years
      @JeffSchaller Fair enough.