M BUZZ CRAZE NEWS
// news

Show 1 to N on the terminal

By Joseph Russell

I am looking for simple thing just, foo 8 will shows this:

1
2
3
4
5
6
7
8

PS: I am looking just for command line. I know how to create that by using for on the bash

2

5 Answers

To print a sequence of number the command 'seq' is your friend

seq 8
3

{1..8} will give you a simple argument range in Bash.

If you need that line by line, I'd suggest feeding that to something like printf:

$ printf '%d\n' {1..8}
1
2
3
4
5
6
7
8
3

You can also use echo command with brace expansion

echo -e "\n"{1..8}
1
2
3
4
5
6
7
8

If you don't want the initial newline, you can use one of the below commands.

echo -e "\n"{1..8}|tail -n8
echo -e "\n"{1..8}|grep .
echo -e "\n"{1..8}|grep [0-9]
echo -e "\n"{1..8}|sed 1d
2

Alternatively you can get it with simplest way as follows:

$ echo {1..8} | tr ' ' '\n'
1
2
3
4
5
6
7
8

OR:

$ for ((i=1 ; i<=8 ; i++)) do echo $i ; done;
1
2
3
4
5
6
7
8

8 can be replaced by your 'N' positive integer!

You could use this simple for command,

$ for i in {1..8}; do echo $i; done
1
2
3
4
5
6
7
8

Through awk,

$ awk 'BEGIN{for(i=1;i<=8;i++) {print i;}}'
1
2
3
4
5
6
7
8
5

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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