Shell Games

Olivier Gautherot olivier at gautherot.net
Fri Dec 31 01:36:16 PST 2004


Hi folks!

>>>FWIW, I've always used 'tcsh' interactively, but almost switched a
>>>while back out of disgust at not being able to figure out how to
>>>get a one-line foreach/{do_something}/end loop (which would allow
>>>me to re-run a complex command easily.)
>>>      
>>>
>>I would recommend 'ksh' it does the tasks of both 'csh' and 'sh' with
>>advanced programming features, check out http://www.kornshell.com/
>>    
>>
>
>As it happens, my attempts to do this one-liner with 'tcsh' were a
>direct result of watching someone who used 'ksh'.  I thought it
>probable that 'bash' also could accomplish this feat (and maybe
>'tcsh' also if I could figure it out.)  Anyone know?
>  
>
The keyword is "for", not "foreach". Try:

bash-2.05b$ for n in tata tete titi toto tutu ; do echo "New item" ; 
echo $n ; done
New item
tata
New item
tete
New item
titi
New item
toto
New item
tutu
bash-2.05b$

Note that the "do" must be followed directly by a command. A semicolon
(i.e. an empty command) is considered a syntax error.

My cent worth ;-)
    Olivier


More information about the freebsd-newbies mailing list