0

How would I echo a string containing a variable, that has a lot of spaces before and after the var?

Desired output is:

|                        BLAH v1.2.3                          |

The $v var is fine, and contains the proper value.

Here's what I have so far:

#!/bin/bash

v=$(cat version.json)

echo '|                        BLAH v${v}                          |'

Update (see comments):

#!/bin/bash

v="$(cat version.json)"

echo "|                        BLAH v${v}                          |"

Results in:

Notice:

  • how the front space shows up before the front pipe
  • the pipe bumps up next to the first word
  • the first 2 letters have vanished into the ether (lol)
  • and (cant tell for sure where the last spaces are there) but the last pipe ended up on the next line.
J. Scott Elblein
  • 4,013
  • 15
  • 58
  • 94
  • 2
    If you want interpolation, use `"` instead of `'`. – Ted Lyngmo Dec 10 '22 at 04:28
  • Sidenote: Do `v="$(cat version.json)"` – Ted Lyngmo Dec 10 '22 at 04:30
  • If for some bizarre reason you want single quotes, you could do `echo '| '${v}' |'`, but even then best practice would be to do `echo '| '"${v}"' |'`, but...you probably just want to put the whole thing in double-quotes. – William Pursell Dec 10 '22 at 04:41
  • @TedLyngmo and @.William Pursell, unfortunately still no luck. I added what happened in the update in the OP. I also tried pasting verbatim both of William's suggestions (no luck), and adding double quotes around the var in the echo, like this `"${v}"`, but same result. – J. Scott Elblein Dec 10 '22 at 07:22
  • 1
    It looks like `version.json` has DOS line endings. See: [Are shell scripts sensitive to encoding and line endings?](https://stackoverflow.com/questions/39527571/are-shell-scripts-sensitive-to-encoding-and-line-endings) – John Kugelman Dec 10 '22 at 07:33

0 Answers0