If you don't have "printf" or "sed" like my Android phone then longhand:-
#!/bin/bash
# spaces.sh
spaces=""
VARIABLE=$(($[ RANDOM % 10 ]+1))
text="Mary walks with her dog."
#
for n in $( seq 1 1 $VARIABLE ); do spaces=$spaces" "; done
#
# At the end.
echo -n "$text$spaces"
# Just a separater colon for clarity.
echo -n ":"
# At the start.
echo "$spaces$text"
# Anywhere you choose.
echo "${text:0:7}$spaces${text:7}"
Results, OSX 10.7.5, default bash terminal.
Last login: Sat Jan 24 19:18:07 on ttys000
AMIGA:barrywalker~> cd Desktop/Code/Shell
AMIGA:barrywalker~/Desktop/Code/Shell> ./spaces.sh
Mary walks with her dog. : Mary walks with her dog.
Mary wa lks with her dog.
AMIGA:barrywalker~/Desktop/Code/Shell> ./spaces.sh
Mary walks with her dog. : Mary walks with her dog.
Mary wa lks with her dog.
AMIGA:barrywalker~/Desktop/Code/Shell> ./spaces.sh
Mary walks with her dog. : Mary walks with her dog.
Mary wa lks with her dog.
AMIGA:barrywalker~/Desktop/Code/Shell> ./spaces.sh
Mary walks with her dog. : Mary walks with her dog.
Mary wa lks with her dog.
AMIGA:barrywalker~/Desktop/Code/Shell> _
In short: this is not possible. The reason is that regular expressions have certain limitations and this is one of them.
If you are interested in a general and very scientific explanation of why this is so: regular expressions are of the same expressive power as regular languages, which themselves are Type-3 languages in the Chomsky-Sch�tzenberger hierarchy. (Look up "Chomsky hierarchy" and "regular language" in Wikipedia or the net for more detail.) An expression of the form <expression> times variable would need a so-called backreference, which is not possible in a regular language because it would not be context-free any more. See the Pumping Lemma, which can be used to prove this.
Having said this: you already got some workarounds of one or the other form. Just be aware that everything you can achieve is more or less clever workarounds for the mentioned limitation.
Hi,
With sed (I just replace space by point for view):
$ echo $VARIABLE
14
$ echo $string
Mary walks at the park every day with her children
$ echo $string | sed -e 'h;z;:bc;s/^\.\{,'$VARIABLE'\}$/&\./;tbc;x;G;s/\n\.//'
Mary walks at the park every day with her children..............
The whole concept of adding a random number of spaces to the end of a string doesn't make any sense to me.
I can see wanting to pad a field with trailing spaces to create a fields that contains exactly X characters: printf '%-*.*s\n' $X $X "$field_contents"
I can see wanting to pad a field with leading spaces to create a field that contains exactly X characters: printf '%*.*s\n' $X $X "$field_contents"
I can see wanting to pad a field with trailing spaces to create a field that is at least X characters (but not truncate the field if it is longer): printf '%-*s\n' $X "$field_contents"
I can see wanting to pdd a field with leading spaces to create a field that is at least X characters (but not truncate the field if it is longer): printf '%*s\n' $X "$field_contents"
Why do you want to add a random number of space???
Still there is an academical interest if this can be done with sed.
Apart from the s/x/y/num that must go from left to right (so can hardly be used on x$ ,
there is x\{num\} , a special case of x\{min,max\} .
Modified the previous idea, and for Unix sed replaced some ; by newline:
NB=14
echo "Mary walks at the park every day with her children" | sed '
:A
/ \{'"$NB"'\}$/b
s/$/ /;bA'
NB instead of newlines one can use several '-e line':
echo "Mary walks at the park every day with her children" | sed -e ':A' -e '/ \{'"$NB"'\}$/b' -e 's/$/ /;bA'