For x in a b c; { list; } (in lieu of do list; done) works (in bash) - why and how? | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

For x in a b c; { list; } (in lieu of do list; done) works (in bash) - why and how?

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-04-2013
RudiC RudiC is offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 30 October 2014, 9:58 AM EDT
Location: Aachen, Germany
Posts: 4,474
Thanks: 73
Thanked 1,097 Times in 1,033 Posts
For x in a b c; { list; } (in lieu of do list; done) works (in bash) - why and how?

In this post I came across the cited construct. It works! while ... { list; } does not.
man bash does not mention it (or, better, I didn't see it).

Any reason for / behind this? Am I missing something?
Sponsored Links
    #2  
Old 02-04-2013
neutronscott's Avatar
neutronscott neutronscott is offline Forum Advisor  
script kiddie
 
Join Date: Jun 2011
Last Activity: 29 October 2014, 4:54 PM EDT
Location: McMurdo Station, Antarctica
Posts: 820
Thanks: 28
Thanked 244 Times in 230 Posts
how about f() without braces?


Code:
mute@clt:~$ f() for x in 1 2 3; { echo $x; };
mute@clt:~$ f
1
2
3

implementation. somehow for got away with accepting a list, but while didn't. I would not rely on it, but it has saved me a few bytes with code golfing
Sponsored Links
    #3  
Old 02-04-2013
Scrutinizer's Avatar
Scrutinizer Scrutinizer is online now Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 30 October 2014, 11:10 AM EDT
Location: Amsterdam
Posts: 9,558
Thanks: 286
Thanked 2,429 Times in 2,176 Posts
I think this undocumented syntax is still there for historical reasons and should not be used. I noticed it works in both old and new bash versions. It also works in zsh and ksh93, but not in ksh88. It is not POSIX compliant and does not work in dash.
    #4  
Old 02-04-2013
alister alister is offline
Registered User
 
Join Date: Dec 2009
Last Activity: 11 June 2014, 8:40 PM EDT
Posts: 3,231
Thanks: 179
Thanked 973 Times in 789 Posts
Shells with that type of for-loop support typically also support an analogous select statement: select i in a b c; { echo $i; }

I have never used either myself. I see no benefit, only headaches.

Regards,
Alister
Sponsored Links
    #5  
Old 02-04-2013
Don Cragun's Avatar
Don Cragun Don Cragun is online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 30 October 2014, 11:01 AM EDT
Location: San Jose, CA, USA
Posts: 4,927
Thanks: 184
Thanked 1,651 Times in 1,402 Posts
Quote:
Originally Posted by Scrutinizer View Post
I think this undocumented syntax is still there for historical reasons and should not be used. I noticed it works in both old and new bash versions. It also works in zsh and ksh93, but not in ksh88. It is not POSIX compliant and does not work in dash.
To be picky: An application using these features is not a strictly conforming POSIX application, but a shell that provides these features is not made non-compliant by providing this feature as an extension to the standard.

An application could be a POSIX conforming application using extensions if it documented that it needed a shell that supports this syntax and otherwise used only features documented by the POSIX standards.

Otherwise, as far as the standards are concerned, the behavior of an application using syntax that is not documented by the standard is unspecified.
Sponsored Links
    #6  
Old 02-05-2013
Scrutinizer's Avatar
Scrutinizer Scrutinizer is online now Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 30 October 2014, 11:10 AM EDT
Location: Amsterdam
Posts: 9,558
Thanks: 286
Thanked 2,429 Times in 2,176 Posts
I like it when you're being picky. With "It is not .." , I meant "The use of these features is not.."
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Take a list if strings from a file and search them in a list of files and report them realspirituals Shell Programming and Scripting 8 10-13-2011 06:37 AM
find list of files from a list and copy to a directory manishabh Shell Programming and Scripting 3 09-13-2009 06:03 PM
Splitting a list @list by space delimiter so i can access it by using $list[0 ..1..2] shriyer Shell Programming and Scripting 1 07-29-2009 02:05 PM
Make a list in bash out of arguments jolecanard Shell Programming and Scripting 4 07-02-2009 04:23 PM
[bash]Stripping lines from a list noratx UNIX for Dummies Questions & Answers 8 04-04-2008 02:38 AM



All times are GMT -4. The time now is 11:10 AM.