Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 883
ciembor Avatar asked Oct 07 '11 23:10

ciembor


2 Answers

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' ' '`
like image 60
Diego Sevilla Avatar answered Oct 23 '22 01:10

Diego Sevilla


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"
like image 1
Fritz G. Mehner Avatar answered Oct 23 '22 02:10

Fritz G. Mehner