ciembor
ciembor

Reputation: 7337

Whitespace concatenation in Bash

The problem is simple.

for i in `seq $begin $progress_length` ; do
  progress_bar=$progress_bar'#'
done

for i in `seq $middle $end` ; do
  empty_space=$empty_space' '
done

I need empty_space to position content after the progress bar. I've expected that it will be string of x whitespaces. But finally string is empty. How may I create string of x whitespaces?

Upvotes: 6

Views: 16553

Answers (3)

ekqnp
ekqnp

Reputation: 394

I understand your problem as I had exactly the same.

My solution was to concatenate a temporary character instead of a whitespace, say for example , and then, at the end, replace all their occurences with sed by a whitespace :

echo $myString | sed 's/☼/ /g'

I hope it will help you !

Upvotes: 1

Diego Sevilla
Diego Sevilla

Reputation: 29021

The problem may be because $empty_space has only spaces. Then, to output them you have to surround it in double quotes:

echo "${empty_space}some_other_thing"

You can try more interesting output with printf for example to obtain several spaces. For instance, to write 20 spaces:

v=`printf '%20s' ' '`

Upvotes: 8

Fritz G. Mehner
Fritz G. Mehner

Reputation: 17198

The strings can be created using parameter substitution. The substitution ${str:offset:length} returns a substring of str :

space80='                                                                                '
hash80='################################################################################'

progress_bar=${hash80:0:$progress_length-$begin+1}
empty_space=${space80:0:$end-$middle+1}

echo -n "$empty_space$progress_bar"

Upvotes: 1

Related Questions