Skip to content
The Chat Archive
gemini-2.5-flash-preview-04-17· Google LMArena Human Preference 140K

linux CLI. how can I repeat 23 times the same value?

  1. User

    linux CLI. how can I repeat 23 times the same value?

  2. Assistant

    Okay, here are several ways to repeat a value 23 times using the Linux command line. The best method depends slightly on your shell (Bash, Zsh, etc.) and preference for conciseness vs. portability.

    We'll assume the value is a string like "HELLO". Replace "HELLO" with your desired value. If it's just a number or simple word, you might not need quotes, but it's good practice.

    1. Using a for loop (Bash/Zsh specific {1..23}): This is often the most straightforward for interactive use in Bash or Zsh.

    VALUE="HELLO"
    for i in {1..23}; do
        echo "$VALUE"
    done
    
    • {1..23}: This is Bash/Zsh brace expansion which generates the sequence 1 2 3 ... 23.
    • for i in ...: Loops through each item in the sequence. The variable i isn't actually used here, it just controls the number of iterations.
    • echo "$VALUE": Prints the value followed by a newline on each iteration.

    2. Using a for loop with seq (More portable): If your shell doesn't support {start..end} or you need more options for the sequence, use seq.

    VALUE="HELLO"
    for i in $(seq 1 23); do
        echo "$VALUE"
    done
    
    • $(seq 1 23): Runs the seq 1 23 command and substitutes its output (the numbers 1 through 23, each on a new line) into the for loop. seq is a standard command.

    3. Using printf and brace expansion (Concise Bash/Zsh): This is a very efficient and concise way in Bash/Zsh using a printf trick.

    VALUE="HELLO"
    printf '%.0s%s\n' {1..23} "$VALUE"
    

    or embedding the value directly:

    printf '%.0sHELLO\n' {1..23}
    
    • {1..23}: Again, generates the sequence 1 to 23.
    • printf '%.0s%s\n': The magic here is %.0s.
      • %s: This is a format specifier that consumes an argument (like "HELLO").
      • %.0s: The .0 means print at most 0 characters from the string argument. So, it consumes the argument ("HELLO" in this case) but prints nothing from it.
      • %s\n: After consuming the number from {1..23} with %.0s, the printf format moves on. It then expects another argument for the second %s. We provide "HELLO" as the second argument once. printf is smart enough to reuse the arguments when the format string repeats.
      • \n: Prints a newline.
    • So, for each number in {1..23}, printf consumes the number with %.0s (printing nothing), then prints the next available argument ("HELLO") followed by a newline.

    4. Using seq and xargs: Another concise way using standard tools.

    VALUE="HELLO"
    seq 1 23 | xargs -I {} echo "$VALUE"
    
    • seq 1 23: Generates the numbers 1 through 23.
    • |: Pipes the output of seq to xargs.
    • xargs: Executes a command using arguments read from standard input.
    • -I {}: Tells xargs to replace the string {} in the command with each line read from input.
    • echo "$VALUE": The command to execute for each line. We don't actually use {}, we just run echo "$VALUE" 23 times.

    5. Using awk: awk is great for text processing but can also generate output from scratch using its BEGIN block.

    VALUE="HELLO"
    awk -v value="$VALUE" 'BEGIN { for (i=1; i<=23; i++) print value }'
    

    or embedding the value directly:

    awk 'BEGIN { for (i=1; i<=23; i++) print "HELLO" }'
    
    • awk ...: Invokes the awk command.
    • -v value="$VALUE": Sets an awk variable value to the shell variable VALUE. (Only needed if using a shell variable).
    • BEGIN { ... }: Code inside the BEGIN block is executed before awk processes any input lines (we don't have any here).
    • for (i=1; i<=23; i++): A standard programming loop that runs 23 times.
    • print value or print "HELLO": Prints the value followed by awk's default output record separator (which is a newline by default).

    Choosing a Method:

    • For simple scripts and interactive use in Bash/Zsh: for i in {1..23}; do echo ... is very readable.
    • For maximum conciseness in Bash/Zsh: printf '%.0sVALUE\n' {1..23} is hard to beat.
    • For portability across different shells: for i in $(seq 1 23); do echo ... or seq 1 23 | xargs -I {} echo ... are good choices.
    • If you're already using awk or prefer a self-contained solution: The awk method works well.

    All these methods will print "HELLO" 23 times, each on a new line.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.

Identical first prompt