5

Sometimes I want to create lots of whitespace at once (slightly different than a specific character). I attempted to do this using a for loop, but I am only printing \n once with this implementation. Furthermore, the actual '\n' character is actually printed instead of a blank line. What is a better way to do this?

for i in {1....100}
> do
>   echo "\n"
> done
kilojoules
  • 9,768
  • 18
  • 77
  • 149
  • 1
    So leave the "\n" off and just use echo with no parameters - echo automatically adds a linefeed unless you use the -n option. – Jerry Jeremiah Feb 12 '16 at 00:05
  • 2
    Also you should have `{1..100}` and not `{1....100}` because four dots doesn't mean anything. – Jerry Jeremiah Feb 12 '16 at 00:06
  • Possible duplicate of [How can I repeat a character in bash?](http://stackoverflow.com/questions/5349718/how-can-i-repeat-a-character-in-bash) – miken32 Feb 12 '16 at 00:13

5 Answers5

13

To print 5 blank lines:

yes '' | sed 5q

To print N blank lines:

yes '' | sed ${N}q
William Pursell
  • 204,365
  • 48
  • 270
  • 300
4
  • Brace expansion expects two dots, not any other number:

    $ echo {1....5}
    {1....5}
    $ echo {1..5}
    1 2 3 4 5
    

    That's why your loop was executed just once.

  • If you want echo to interpret your escape sequences, you need to call it with echo -e.
  • echo outputs a newline anyway, so echo -e "\n" prints two newlines. To prevent echo from printing a newline you have to use echo -n, and echo -ne "\n" is the same as just echo.
  • You can print repeating characters, in this case a newline, like this:

    printf '\n%.0s' {1..100}
    
Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
3

As Jerry commented you made a syntax error.

This seems to work :

for i in {1..100}
do
    echo "\n"
done
Telemachus
  • 19,459
  • 7
  • 57
  • 79
AntoineG
  • 106
  • 1
  • 1
  • 7
1

You can abuse printf for that:

printf '\n%.s' {1..100}

This prints \n followed by a zero-length string 100 times (so effectively, \n 100 times).

One caveat of using brace expansion though is that you cannot use variables in it, unless you eval it:

count=100
eval "printf '\n%.s' {1..$count}"

To avoid the eval you can use a loop; it's slightly slower but shouldn't mater unless you need thousands of them:

count=100
for ((i=0; i<$count; i++)); do printf '\n'; done

NB: If you use the eval method, make sure you trust your count, else it's easy to inject commands into the shell:

$ count='1}; /bin/echo "Hello World ! " # '
$ eval "printf '\n%.s' {1..$count}"

Hello World ! 

One easy fix in Bash is to declare your variable as an integer (ex: declare -i count). Any attempts to pass something else than an number will fail. It may still be possible to trigger DOS attacks by passing very large values for the brace expansion, which may cause bash to trigger an OOM condition.

Thomas Guyot-Sionnest
  • 2,251
  • 22
  • 17
1

Try either of the following

for i in {1..100}; do echo ; done

Or

for i in {1..100}
do 
    echo
done

Worked for Ubuntu 16LTS and MacOS X

Gayal Kuruppu
  • 1,261
  • 1
  • 17
  • 29