# Efficiently Repeat Text

**URL:** <https://community.unix.com/t/efficiently-repeat-text/264425>\
**Category:** UNIX for Dummies Questions & Answers\
**Created:** [April 22, 2010, 7:42pm UTC](https://community.unix.com/t/efficiently-repeat-text/264425 "2010-04-22T19:42:57Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![msb65](https://community.unix.com/user_avatar/community.unix.com/msb65/32/4365_2.png) [@msb65](https://community.unix.com/u/msb65)\
**Post date:** [April 22, 2010, 7:42pm UTC](https://community.unix.com/t/efficiently-repeat-text/264425/1 "2010-04-22T19:42:57Z")

</div>

Hi,

Often when I use echo statements in scripts I echo a line of #'s above and below. For example:

```nohighlight
echo #####
echo hello world
echo #####

```

However, I generally have a series of about 75 #'s. For example:

```nohighlight
echo #(x 75)
echo hello world
echo #(X 75)

```

While this helps to delineate sections in my terminal output, my code is filled with lines of #'s. Is there a way to tell the echo command to repeat a # 75 times, so I don't have to type them individually?

---

<div class="post-metadata">

**Author:** ![Scott](https://community.unix.com/letter_avatar/scott/32/6_449c533f586939b6e5f246632a46421f.png) [@Scott](https://community.unix.com/u/Scott)\
**Post date:** [April 22, 2010, 8:01pm UTC](https://community.unix.com/t/efficiently-repeat-text/264425/2 "2010-04-22T20:01:29Z")

</div>

Hi.

There is a few good answers here: [http://www.unix.com/shell-programming-scripting/46584-repeat-character-printf.html](http://www.unix.com/shell-programming-scripting/46584-repeat-character-printf.html)

Modifying post #8 from vgersh99 slightly to fit your needs:

```plaintext
awk 'BEGIN{$75=OFS="#";print}'

```

---

<div class="post-metadata">

**Author:** ![DeCoTwc](https://community.unix.com/letter_avatar/decotwc/32/6_449c533f586939b6e5f246632a46421f.png) [@DeCoTwc](https://community.unix.com/u/DeCoTwc)\
**Post date:** [April 22, 2010, 11:15pm UTC](https://community.unix.com/t/efficiently-repeat-text/264425/3 "2010-04-22T23:15:23Z")

</div>

What I do in many of my scripts is create a function at the beginning called separator and then whenever I need to separate things, I just call on it.

```nohighlight
$cat example
separator () #Function that draws separator
    {
        printf "${LB}==============================================================================================================${N}\n"
    }
separator 
echo "This is an example"
separator 

$./example
==============================================================================================================
This is an example
==============================================================================================================

```

Edit: The {LB} and {N} are there because in the script I copied this out of, I was using ANSI colour codes to colour the separator.

---

<div class="post-metadata">

**Author:** ![DeCoTwc](https://community.unix.com/letter_avatar/decotwc/32/6_449c533f586939b6e5f246632a46421f.png) [@DeCoTwc](https://community.unix.com/u/DeCoTwc)\
**Post date:** [April 23, 2010, 2:00pm UTC](https://community.unix.com/t/efficiently-repeat-text/264425/5 "2010-04-23T14:00:37Z")

</div>

> [@thegeek](#):
>
> When you can write function, you can do lot of optimization such as
> 
> ```plaintext
> separator()
> {
> for (( i=0 ; i<$1 ; i++ )) ;
> do
> echo -n "#"
> done
> }
> 
> separator 10;
> 
> ```

Sorry to thread jack. I'm pretty familiar with bash functions, and I use them regularly in my scripting, but I was wondering if you could just give me an idiots guide of the loop you wrote there. I understand what it's doing, but I don't really know the C style i++...etc notation.

I know I could get the same results with:

```plaintext
sep ()
{
    i=0;
    while [[$i -lt $1]]; do
        echo -n "#";
        i=$(($i + 1));
    done
}
[~]$ sep 10
##########

```

But I'd like to understand your way (and know if there is an advantage of yours over mine).

---

<div class="post-metadata">

**Author:** ![treesloth](https://community.unix.com/letter_avatar/treesloth/32/6_449c533f586939b6e5f246632a46421f.png) [@treesloth](https://community.unix.com/u/treesloth)\
**Post date:** [April 23, 2010, 4:04pm UTC](https://community.unix.com/t/efficiently-repeat-text/264425/6 "2010-04-23T16:04:35Z")

</div>

Combining the other methods into a slightly shorter idea:

```nohighlight
separator()
{
awk -v num=$1 'BEGIN{$num=OFS="#";print}'
}

separator 10;

```

---

<div class="post-metadata">

**Author:** ![Corona688](https://community.unix.com/letter_avatar/corona688/32/6_449c533f586939b6e5f246632a46421f.png) [@Corona688](https://community.unix.com/u/Corona688)\
**Post date:** [April 23, 2010, 4:16pm UTC](https://community.unix.com/t/efficiently-repeat-text/264425/7 "2010-04-23T16:16:31Z")

</div>

> [@decotwc](#):
>
> Sorry to thread jack. I'm pretty familiar with bash functions, and I use them regularly in my scripting, but I was wondering if you could just give me an idiots guide of the loop you wrote there. I understand what it's doing, but I don't really know the C style i++...etc notation.

```plaintext
#!/bin/bash
# The first expression is evaluated once, before the loop is entered.
# Usually used for assignments. You could put more than one, separated
# by commas, but that's almost never done.

# The second is a condition. Whenever it's true, it continues looping.
# If you put 0, the inner loop would never be executed at all. If you put 1, it'd loop forever.

# The third expression is evaluated after every loop. Usually used to
# increase a value but you could put any expression there.

# the do/done section is just like a while.
for (( N=0 ; N<10 ; N++ ))
do
        echo "ASDF"
done

```

These structures work in bash and newer versions of ksh, but don't work in minimal or old implementations of sh.

---------- Post updated at 02:16 PM ---------- Previous update was at 02:14 PM ----------

> [@treesloth](#):
>
> Combining the other methods into a slightly shorter idea:
> 
> ```plaintext
> separator()
> {
> awk -v num=$1 'BEGIN{$num=OFS="#";print}'
> }
> 
> separator 10;
> 
> ```

Forking off an external process to do it for you is not an efficient way to print ten hatch marks.

Personally, I'd just do:

```plaintext
SEPARATOR="##############"

```

then just "echo $SEPERATOR" whenever I need to.

---

<div class="post-metadata">

**Author:** ![treesloth](https://community.unix.com/letter_avatar/treesloth/32/6_449c533f586939b6e5f246632a46421f.png) [@treesloth](https://community.unix.com/u/treesloth)\
**Post date:** [April 23, 2010, 4:34pm UTC](https://community.unix.com/t/efficiently-repeat-text/264425/8 "2010-04-23T16:34:48Z")

</div>

> [@corona688](#):
>
> Forking off an external process to do it for you is not an efficient way to print ten hatch marks.

No, it isn't, but recall that that's not the point of doing it that way.

---

<div class="post-metadata">

**Author:** ![Corona688](https://community.unix.com/letter_avatar/corona688/32/6_449c533f586939b6e5f246632a46421f.png) [@Corona688](https://community.unix.com/u/Corona688)\
**Post date:** [April 23, 2010, 4:52pm UTC](https://community.unix.com/t/efficiently-repeat-text/264425/9 "2010-04-23T16:52:10Z")

</div>

> [@treesloth](#):
>
> No, it isn't, but recall that that's not the point of doing it that way.

The thread topic mentions efficiency but I suppose that has several meanings, but I'm afraid I still don't understand. More efficient as in less redundancy? Calling function 10 times seems about as redundant as calling echo 10 times.

Sometimes I do something like:

```plaintext
function section
{
        echo "####${1}####"
        shift
        "$@"
}

...

section "echo test" echo asdf
section "Check servers" pester server1 server2 server3 2> errlog
section "Errors" report_relevant_errors < errlog
section "Classified Information" pacify weasels

for F in a b c d e f g
do
        section "Category $F" categorize "$F"
done

```
