Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Is there any built-in linux command that allows to output a string that is n times an input string??

share|improve this question
    
By "built-in linux command" I assume you mean shell command, and since you don't mention which shell you're using, I assume it's bash. You can check this by typing "echo $SHELL" at the command line and you should get something similar to "/bin/bash" back. If you don't, you should edit your answer to specify what it does show. Cheers :) –  Adrian Petrescu Dec 22 '09 at 3:12
4  
I tagged the question with "bash". I thought that would've been enough. –  GetFree Dec 22 '09 at 3:27

14 Answers 14

up vote 28 down vote accepted
adrian@Fourier:~$ printf 'HelloWorld\n%.0s' {1..5}
HelloWorld
HelloWorld
HelloWorld
HelloWorld
HelloWorld
adrian@Fourier:~$
share|improve this answer
    
Could you explain how this works? I understand the printf command since it's the same as the one in C/C++. But I dont understand how the {1..5} is expanded and how that works in conjunction with the "%.0s" part. –  GetFree Dec 22 '09 at 3:24
1  
It's a bit of a hack :) Try running "printf 'HelloWorld %d\n' 1 2 3 4 5" and it'll probably click for you. The %.0s flag is meant to do nothing, just be there to pick up arguments. Then, if bash gets more arguments than it has format specifiers, it will simply print out multiple copies, grabbing as many as it needs. So you get the effect. The output of "printf 'HelloWorld %d%d\n' 1 2 3 4 5 6" probably makes it even clearer. Hope this helps! –  Adrian Petrescu Dec 22 '09 at 3:44
    
I see. It's the particular behavior of printf what allows the repetition –  GetFree Dec 22 '09 at 3:53
1  
(I should note that, from a purely theoretical standpoint, this is probably the fastest solution since it uses a single shell primitive -- not any external processes. In reality though, let's face it, performance of bash scripts doesn't really matter :) –  Adrian Petrescu Dec 22 '09 at 3:53
    
NB: The expansion of {x..y} is not specific to printf. This is a bash syntax that expands to a sequence of numbers x, x+1, ..., y. For example, try "echo {1..10}". This is in the "Brace Expansion" section of the bash man page (and is called a "sequence expression"). –  larsks Dec 22 '09 at 14:25

Here's an old-fashioned way that's pretty portable:

yes "HelloWorld" | head -n 10

This is a more conventional version of Adrian Petrescu's answer using brace expansion:

for i in {1..5}
do
    echo "HelloWorld"
done

That's equivalent to:

for i in 1 2 3 4 5

This is a little more concise and dynamic version of pike's answer:

printf -v spaces '%*s' 10 ''; printf '%s\n' ${spaces// /ten}
share|improve this answer
    
How do you get rid of the new lines when using the yes command? –  GetFree Dec 22 '09 at 4:16
2  
One way is to pipe it through sed -n 'H;${x;s/\n//gp}' or sed -n ':t;${s/\n//gp};N;bt' another is to do echo $(yes "HelloWorld" | head -n 10) which adds a space between each copy of the string. Yet another way is to pipe it through tr -d '\n' which also eliminates the final newline. –  Dennis Williamson Dec 22 '09 at 9:08
    
It's like Haskell in a bash shell: Hashell? –  WChargin Jan 15 at 5:20

Perhaps another way that is more general and useful for you:

adrian@Fourier:~$ n=5
adrian@Fourier:~$ for (( c=1; c<=n; c++)) ; do echo "HelloWorld" ; done
HelloWorld
HelloWorld
HelloWorld
HelloWorld
HelloWorld
adrian@Fourier:~$

The bash shell is more powerful than most people think :)

share|improve this answer
    
This gets my vote, as it's entirely shell-internal; no forking required. –  esm Dec 23 '09 at 18:08
1  
What forking are you referring to? The original answer requires none. The output of 'type printf' is 'printf is a shell builtin' and therefore runs within the original bash process. –  CodeGnome Apr 17 '12 at 20:17

Quite a few good ways already mentioned. Can't forget about good old seq though:

[john@awesome]$for i in `seq 5`; do echo "Hi";done
Hi
Hi
Hi
Hi
Hi
share|improve this answer

This can be parameterized and doesn't require a temp variable, FWIW:

printf "%${N}s" | sed 's/ /blah/g'

Or, if $N is the size of a bash array:

echo ${ARR[@]/*/blah}
share|improve this answer
    
This is the shortest POSIX 7 solution I have seen so far since seq, yes and {1..5} are not POSIX 7. –  Ciro Santilli Apr 10 at 11:27

repeat n times, just put n-1 , in {}:

$ echo 'helloworld'{,,}
helloworld helloworld helloworld
share|improve this answer
1  
Nice, but what if I want to get n from a variable? –  GetFree Jun 28 '12 at 7:32

You can use a trick. Echoing an empty variable does not print anything. So you can write:

echo word$wojek{1..100}

If $wojek1 $wojek2 ... $wojek100 are non-existing variables you will get your word repeated 100 times without anything else.

share|improve this answer
awk 'BEGIN {while (c++<4) printf "Hello"}'

Result

HelloHelloHelloHello
share|improve this answer
line="==========================="
line=${line:0:10}
${line//"="/"ten "}

outputs

ten ten ten ten ten ten ten ten ten ten
share|improve this answer
    
Maybe a more verbose example: declare c='-----'; c=${c//${c:0:1}/$c}; echo $c # Prints "-" 25 times. –  Stephen Niedzielski Jan 2 '13 at 23:02
watch -n# 'command'

Where # is the number in seconds you want the command to be repeated.

share|improve this answer

Assuming you want something like Perl's x operator, where you don't automatically get a newline between repetitions:

x() {
  # usage: x string num
  for i in $(seq 1 $2); do printf "%s" "$1"; done
  # print a newline only if the string does not end in a newline
  [[ "$1" == "${1%$'\n'}" ]] && echo ""
}

x Hi 10  # ==> HiHiHiHiHiHiHiHiHiHi

x $'Hello World!\n' 3

I explicitly used a for loop because you can't write {1..$n} in bash: brace expansion is done before variable substitution.

share|improve this answer

Try this one:

echo $(for i in $(seq 1 100); do printf "-"; done)

Will create (a hundred dash):


share|improve this answer

based on what @pike was hinting at

for every character in string echo string

echo ${target//?/$replace}

An example of a heading underlined with = characters

export heading='ABCDEF'; 
export replace='='; 
echo -e "${heading}\n${heading//?/$replace}"

will output

ABCDEF
======

This seems to port between linux and OS X and that makes me happy.

nJoy!

share|improve this answer

Not exactly built in to linux, but if you have python installed..

python
>>>var = "string"
>>>var*n

Or in one line, as commenter suggested:

python -c 'print "This is a test.\n" * 10'
share|improve this answer
3  
Not really that useful, since it can't be easily integrated into shell scripts, etc. And since there's about a billion ways to do this in the shell itself, I see little reason to bring out the big guns (i.e Python) for it. –  Adrian Petrescu Dec 22 '09 at 3:18
5  
I agree with your second point but not the fist...it's easy to integrate into a shell script: python -c 'print "This is a test.\n" * 10' –  larsks Dec 22 '09 at 14:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.