Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and 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 Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 02-04-2013
RudiC RudiC is offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 25 May 2015, 6:46 AM EDT
Location: Aachen, Germany
Posts: 5,959
Thanks: 106
Thanked 1,592 Times in 1,496 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 Unix and Linux 02-04-2013
neutronscott's Unix or Linux Image
neutronscott neutronscott is offline Forum Advisor  
script kiddie
 
Join Date: Jun 2011
Last Activity: 24 May 2015, 10:23 PM EDT
Location: South Carolina, USA
Posts: 868
Thanks: 29
Thanked 266 Times in 252 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 Linux
Sponsored Links
    #3  
Old Unix and Linux 02-04-2013
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 25 May 2015, 3:21 PM EDT
Location: Amsterdam
Posts: 10,211
Thanks: 333
Thanked 2,694 Times in 2,410 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 Unix and Linux 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 974 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 Unix and Linux 02-04-2013
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 25 May 2015, 9:28 PM EDT
Location: San Jose, CA, USA
Posts: 6,369
Thanks: 266
Thanked 2,130 Times in 1,821 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 Unix and Linux 02-05-2013
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 25 May 2015, 3:21 PM EDT
Location: Amsterdam
Posts: 10,211
Thanks: 333
Thanked 2,694 Times in 2,410 Posts
Linux I like it when you're being picky. With "It is not .." , I meant "The use of these features is not.."
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 09:34 PM.