Linux-Befehl, um eine Zeichenfolge n mal zu wiederholen

Gibt es einen integrierten Linux-Befehl, mit dem eine Zeichenfolge ausgegeben werden kann, die n-mal eine Eingabezeichenfolge ist??

Author: Steven Penny, 2009-12-22

16 answers

adrian@Fourier:~$ printf 'HelloWorld\n%.0s' {1..5}
HelloWorld
HelloWorld
HelloWorld
HelloWorld
HelloWorld
adrian@Fourier:~$
 97
Author: Adrian Petrescu,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/techietown.info/template/agent.layouts/content.php on line 61
2009-12-22 03:08:46

Hier ist eine altmodische Art, das ist ziemlich tragbar:

yes "HelloWorld" | head -n 10

Dies ist eine konventionellere Version von Adrian Petrescu ' s Antwort mit brace expansion:

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

Das entspricht:

for i in 1 2 3 4 5

Dies ist eine etwas prägnantere und dynamischere Version von pikes Antwort:

printf -v spaces '%*s' 10 ''; printf '%s\n' ${spaces// /ten}
 83
Author: Dennis Williamson,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/techietown.info/template/agent.layouts/content.php on line 61
2012-05-08 20:48:07

Dies kann parametriert werden und erfordert keine temporäre Variable, FWIW:

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

Oder, wenn $N die Größe eines Bash-Arrays ist:

echo ${ARR[@]/*/blah}
 15
Author: twifkak,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/techietown.info/template/agent.layouts/content.php on line 61
2012-05-08 01:26:09

Einige gute Wege bereits erwähnt. Ich kann das gute alte seq jedoch nicht vergessen:

[john@awesome]$for i in `seq 5`; do echo "Hi";done
Hi
Hi
Hi
Hi
Hi
 14
Author: John T,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/techietown.info/template/agent.layouts/content.php on line 61
2009-12-22 16:20:09

Sie können einen Trick verwenden. Das Echo einer leeren Variablen druckt nichts. So können Sie schreiben:

echo word$wojek{1..100}

Wenn $wojek1 $wojek2 ... $wojek100 sind nicht vorhandene Variablen Sie erhalten Ihr Wort 100 Mal wiederholt, ohne etwas anderes.

 13
Author: Wojtek Waga,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/techietown.info/template/agent.layouts/content.php on line 61
2012-10-16 13:27:54

Vielleicht ein anderer Weg, der allgemeiner und nützlicher für Sie ist:

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

Die Bash-Shell ist mächtiger als die meisten Leute denken:)

 11
Author: Adrian Petrescu,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/techietown.info/template/agent.layouts/content.php on line 61
2009-12-22 03:11:03

Wiederholen Sie n mal, setzen Sie einfach n-1 Kommas zwischen {}:

$ echo 'helloworld'{,,}
helloworld helloworld helloworld

, Wiederholt 'helloworld', zweimal nach dem ersten echo.

 7
Author: kev,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/techietown.info/template/agent.layouts/content.php on line 61
2019-02-01 17:28:45

POSIX-AWK:

#!/usr/bin/awk -f
function str_repeat(s1, n1) {
   s2 = ""
   for (n2 = 1; n2 <= n1; n2++) {
      s2 = s2 s1
   }
   return s2
}
BEGIN {
   s3 = str_repeat("Sun", 5)
   print s3
}

- Oder PHP -:

<?php
$s3 = str_repeat('Sun', 5);
echo $s3, "\n";
 6
Author: Steven Penny,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/techietown.info/template/agent.layouts/content.php on line 61
2020-02-24 18:37:49

Ich habe fehlerhafte Pipe-Warnungen mit der yes - Lösung erlebt, also hier ist eine weitere gute Alternative:

$ seq 4 | sed "c foo"
foo
foo
foo
foo
 4
Author: n.caillou,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/techietown.info/template/agent.layouts/content.php on line 61
2017-08-15 23:54:26

Basierend auf dem, was @pike angedeutet hat

Für jedes Zeichen im string-echo string

echo ${target//?/$replace}

Ein Beispiel für eine Überschrift, die mit = Zeichen unterstrichen ist

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

Gibt

ABCDEF
======

Dies scheint zwischen Linux und OS X zu portieren und das macht mich glücklich.

NJoy!

 3
Author: nickl-,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/techietown.info/template/agent.layouts/content.php on line 61
2013-02-28 17:21:40

Wenn Sie auf BSD sind, können Sie einfach seq.

$ seq -f "Hello, world" 5
Hello, world
Hello, world
Hello, world
Hello, world
Hello, world
 3
Author: Qix - MONICA WAS MISTREATED,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/techietown.info/template/agent.layouts/content.php on line 61
2017-03-07 19:14:47

Keine Magie hier:

seq 5 | awk '{print "Hello World"}'

 3
Author: brablc,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/techietown.info/template/agent.layouts/content.php on line 61
2018-06-15 07:46:58
line="==========================="
line=${line:0:10}
${line//"="/"ten "}

- Ausgänge

ten ten ten ten ten ten ten ten ten ten
 2
Author: commonpike,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/techietown.info/template/agent.layouts/content.php on line 61
2010-08-11 08:21:35

Angenommen, Sie möchten so etwas wie den Operator x von Perl, bei dem Sie zwischen den Wiederholungen nicht automatisch eine neue Zeile erhalten:

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

Ich habe explizit eine for Schleife verwendet, da Sie {1..$n} in bash nicht schreiben können: Die Erweiterung der Klammer erfolgt vor der Variablensubstitution.

 2
Author: glenn jackman,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/techietown.info/template/agent.layouts/content.php on line 61
2011-07-04 12:02:10

Versuchen Sie dieses:

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

Erzeugt (hundert Strich):


 2
Author: Alle Aldine,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/techietown.info/template/agent.layouts/content.php on line 61
2012-07-11 14:43:46

Nicht genau in Linux eingebaut, aber wenn Sie Python installiert haben..

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

Oder in einer Zeile, wie der Kommentator vorgeschlagen hat:

python -c 'print "This is a test.\n" * 10'
 2
Author: eqzx,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/techietown.info/template/agent.layouts/content.php on line 61
2014-06-02 20:12:17