5

echo is used to produce a new line in Linux.

echo -e '\n' 2 lines

echo -e '\n\n' 3 lines and so on.

e.g.

user@linux:~$ echo

user@linux:~$ echo -e '\n'


user@linux:~$ echo -e '\n\n'



user@linux:~$ 

Instead of typing \n\n, would it be possible to use something like \n * 3.

Desired Output

I know echo -e '\n * 3' won't produce 3 lines, but I hope you get what I mean.

user@linux:~$ echo -e '\n * 3' # <== I hope you get what I mean



user@linux:~$ 
  • 3
    yes '' | head -n3, printf '%.0s\n' {1..3} – mosvy Mar 29 at 6:43
  • 4
    perl -e 'print "\n" x 3' – Kusalananda Mar 29 at 7:08
  • 4
    The comma is not part of the command -- it's not even highlighted and it should show in a different font ;-) The yes '' | head -n3 and printf '%.0s\n' {1..3} are 2 different commands, both doing the same thing. – mosvy Mar 30 at 5:33
  • 2
    You should accept icarus's answer, which is similar. – mosvy Mar 30 at 11:10
  • 1
    @Sabrina Added some (:-)) alternatives. – Isaac Mar 30 at 23:21
8

If a POSIX solution is required, use:

loop

Beside the obvious (and ugly looking) POSIX loop:

i=0; while [ "$((i+=1))" -le 7 ]; do echo; done

POSIX (portable)

There are other (shorter, not necessarily faster) solutions:

(1) Print spaces, convert to newlines.

printf '%*s' 30 | tr ' ' '\n'

(2) Or, to get 23 lines

seq 23 | tr -dc '\n'

(3) Using awk:

seq 23 | awk '{printf "\n"}'

(4) Assuming IFS is default

printf '%.0s\n' $(seq 23)

(5) sed appends one additional newline, for 23 newlines, use 22:

printf '%0*d' 22 | sed 'y/0/\n/'

(6) Some shells auto create all the lower numbered array elements.

zsh -c 'a[33]=1; printf "%.0s\n" "${a[@]}"'

(7) Get some NUL bytes

head -c 5 /dev/zero | tr '\0' '\n'

widely known

Those are beside the more widely known (and used) solutions:

(8) Use head counting:

yes '' | head -n 23

(9) Not POSIX. For some shells (ksh, bash, zsh at least):

printf '%.0s\n' {1..23}

other languages

And, of course, using (non POSIX) higher level languages:

(10) Perl

perl -e 'print "\n" x 23'
perl -E 'say "\n" x 22'  

(11) PHP

php -r 'echo str_repeat( "\n" , 3 );'

(12) Python

python -c 'print("\n" * 3)'
| improve this answer | |
13

If you are using bash or ksh93 or zsh then

printf '%.0s\n' {1..3}

will produce 3 newlines. The {1..3} expands to 1 2 3, and then the printf outputs these as zero width strings followed by newline.

| improve this answer | |
7
$ yes "" | head -30

to get you 30 newlines.

| improve this answer | |
  • 3
    head -30 is not portable. Use head -n30 or sed 30q. – mosvy Mar 30 at 11:15
0

In case using python is an option, the following command behaves the way you want:

python -c 'print("\n" * 3)'
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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