What’s the difference between SEQ and FOR I IN in shell script

Asked

Viewed 91 times

2

I started studying shell script recently and gave me 2 examples, and I would like to know the difference of the commands, when it is better to use one or the other. The example was seq 1 5| and the other was for i in 1 2 3 4 5, both will make a loop from 1 to 5, are the same thing?

  • Alan, has the answer below solved your question or missed something? If it solved the problem, you can accept it, see here how and why to do it. It is not mandatory, but it is a good practice of the site, to indicate to future visitors that it solved the problem. Don’t forget that you can also vote in response, if it has found it useful. If not solved, say what is missing that if it is the case I edit adding more information (as long as it does not deviate from the main subject, which is the difference between the 2 commands)

1 answer

2

In their specific example, both are similar. But they are not exactly the same thing.

seq is used to generate a sequence of numbers, and nothing else. Ex:

# apenas o valor final (inicial é 1)
seq 5
1
2
3
4
5

# valor inicial e final
seq 3 5
3
4
5

# valor inicial, passo e valor final (números de 2 a 10, pulando de 3 em 3)
seq 2 3 10
2
5
8

The output of the command is the numbers, one on each line.


Already the for iterates by the parameters you enter (which may be the numbers, but we’ll see below that’s not limited to that), and you can do anything with them (not just print). Ex:

for i in 1 2 3 4 5
do
  cp arquivo$i.txt pasta
done

That is, the for above is copying the files arquivo1.txt, arquivo2.txt, etc (up to the arquivo5.txt) to a folder.

The for can even be used together with seq, if you want to iterate by the numbers:

# iterar de 1 a 5
for i in $(seq 1 5)
do
  # faz algo com $i
done

The syntax $( ) is the command substitution, that basically takes the output of the command that is between the parentheses and passes them as parameters to the for.

Although you can also use for i in {1..5} to iterate from 1 to 5.


But as I said earlier, the for is not limited to numbers.

For example, if I do for i in *.c, it will iterate through all the file names that end with .c in the current directory.

And I can use the command substitution to place any command, and the for will iterate through the output of the same:

for i in $(um comando qualquer)
do
  # faz algo com $i
done

In this case, the output of the command is passed to the for, iterates for them. The detail is that by default it uses as separator a character that is space, TAB or line break.

For example, let’s assume that the output of the command is the 3 lines below:

lorem ipsum
dolor sit amet
bla bla bla

If I make one for i in $(comando), the above output is passed to the for and with each iteration, each word is set to the variable i.
That is to say, for i in $(comando); do echo $i; done will print:

lorem
ipsum
dolor
sit
amet
bla
bla
bla

But if you want each row to be considered a single parameter, just change the value of the variable IFS:

# usa apenas a quebra de linha como separador
IFS=$'\n'
for i in $(comando) # a cada iteração, $i será uma das linhas
do
  echo $i
done

Now at every iteration, i will be one of the lines, and the output is:

lorem ipsum
dolor sit amet
bla bla bla

Browser other questions tagged

You are not signed in. Login or sign up in order to post.