8

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
  • Are you saying you know the seq command (or brace expansion {1..N}) and are looking for something else?
    – muru
    Jul 25, 2014 at 12:04
  • No I say I know how show them via wrting a bash file. But I do not know there is 'seq' command. Jul 25, 2014 at 12:06

5 Answers 5

30

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

seq 8
3
  • 2
    Also, seq 5 10 prints 5 6 7 8 9 10, and seq 8 16 2 prints 8 10 12 14 16 (increments by 2 each step)
    – user210606
    Jul 25, 2014 at 16:47
  • 6
    @professorfish FOr the second, it's seq 8 2 16, with the increment between first and last. seq 8 16 2 prints nothing. (I always get it wrong too.)
    – Cascabel
    Jul 26, 2014 at 0:41
  • seq is widely available in many Unices, and is part of coreutils in Linux, but it's not POSIX and not portable. Jul 26, 2014 at 16:56
16

{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
  • in here if 8 is a parameter. what I should write ? I mean: printf '%d\n' {0..a} ? thanks. Aug 14, 2014 at 14:19
  • I try $a and ${a} and ... Aug 14, 2014 at 14:26
  • 1
    @lion You have to re-evaluate the line because Bash won't expand. It becomes something like n=5; printf '%d\n' $(eval echo {0..$n})
    – Oli
    Aug 14, 2014 at 14:37
7

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
  • Just a heads up, the output starts with a newline, which may need to be trimmed out (perhaps with tail?) if you use the generated list of numbers in a script.
    – IQAndreas
    Jul 27, 2014 at 4:40
  • 1
    @IQAndreas I have made a edit and added all commands I could think of to remove the newline. Jul 27, 2014 at 12:34
5

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!

4

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
  • The OP specified: I know how to create that by for on the bash. Use while to bring something new. Jul 26, 2014 at 12:01
  • @RaduRădeanu added awk command. Jul 26, 2014 at 16:54
  • 1
    It is still using for :) Jul 26, 2014 at 17:00
  • OMG, i think he doesn't mean the awk for command. Jul 26, 2014 at 17:01
  • And also I think he doesn't mean such a long command... WOE to use bla bla bla ... print i ... bla bla when you can only use only printf '%d\n' {1..8}? Jul 26, 2014 at 17:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .