I have a variable n
, which is a number, and I'm trying to produce an output like below for n=10
:
10
1 1 1 1 1 1 1 1 1 1
So the first line is n
, and the second line consists of n
characters, in this example 1
.
I know I can produce the second line with a command line this:
$ n=10
$ printf '1 %.0s' {1..$n}
1 1 1 1 1 1 1 1 1 1 %
So I can get the output I want with this series of commands:
$ n=10
$ printf "$n\n"; printf '1 %.0s' {1..$n}; printf '\n'
10
1 1 1 1 1 1 1 1 1 1
But I don't know how I can combine these commands into one printf
command. The reason for emphasis on being one command is that I'm trying to redirect this output to another command for processing.
I was gonna ask the question on Unix Stackexchange, but I saw in the manual of printf
that it works the same as the one in C
, so I figured here I might get better results.
FORMAT controls the output as in C printf.
Use {}
to group all the commands so you can redirect them all to the next command.
{ printf "$n\n"; printf '1 %.0s' {1..$n}; printf '\n'; } | some_other_command