Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is a separator not required in `for i do cmd; done` [duplicate]

for i do echo $i; done

How is this even legal? (I would expect it to be written with an extra semi-colon for i; do echo $i; done) It works in bash, dash, zsh, and ksh. The standard (by which I mean http://pubs.opengroup.org/onlinepubs/9699919799/) states:

The for loop requires that the reserved words do and done be used to
delimit the sequence of commands.  The format for the for loop is as follows:

    for name [ in [word ... ]]
    do
        compound-list
    done

So clearly when "in word" is omitted, do is serving as a separator. So the implication seems to be that the separator (the newline) after the [ in [word .. ]] actually belongs inside the closing right bracket. Can someone point to anything in the standard which justifies this (IMO) horrible abuse of the language?

like image 302
William Pursell Avatar asked Apr 20 '18 13:04

William Pursell


People also ask

How do I fix a makefile missing separator?

So if I remember correctly more people than just me were having problems with a certain make file error stating a missing separator, even if you specifically used tabs and such today I realized all you must do is set your indent type in settings to be tabs instead of spaces and then it should work.

What is %% f in batch script?

By default, /F breaks up the command output at each blank space, and any blank lines are skipped.

What does D mean in cmd?

Change Directory - Select a Folder (and drive) Syntax CD [/D] [drive:][path] CD [..] Key /D : change the current DRIVE in addition to changing folder.

How do I set environment variable in cmd?

2.2 Set/Unset/Change an Environment Variable for the "Current" CMD Session. To set (or change) a environment variable, use command " set varname=value ". There shall be no spaces before and after the '=' sign. To unset an environment variable, use " set varname= ", i.e., set it to an empty string.


1 Answers

If you look at the GNU man page, you see the loop has this syntax:

for

    The syntax of the for command is:

    for name [ [in [words …] ] ; ] do commands; done

So as you can see the extra semi-colon is part of the optional section.


The linux-die man page states the same.

like image 63
whoan Avatar answered Oct 05 '22 15:10

whoan