Efficiently Repeat Text

Hi,

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

echo #####
echo hello world
echo #####

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

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?

Hi.

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

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

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

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.

$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.

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:

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).

Combining the other methods into a slightly shorter idea:

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

separator 10;
#!/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 ----------

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:

SEPARATOR="##############"

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

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:

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