Monday, 26 August 2013

Command substitution in for loop not working

Command substitution in for loop not working

I want to keep all files not ending with .bat
I tried
for f in $(ls | egrep -v .bat); do echo $f; done
and
for f in $(eval ls | egrep -v .bat); do echo $f; done
But both approaches yield the same result, as they print everything.
Whereas ls | egrep -v .bat and eval ls | egrep -v .bat work per se, if
used apart from the for loop.



Feel free to edit the question title, as I was not sure what the problem is.
I'm using GNU bash, version 4.1.10(4)-release (i686-pc-cygwin).



Example
$ ls -l | egrep -v ".bat"
total 60K
-rwx------+ 1 SYSTEM SYSTEM 5.3K Jun 6 20:31 fsc*
-rwx------+ 1 SYSTEM SYSTEM 5.3K Jun 6 20:31 scala*
-rwx------+ 1 SYSTEM SYSTEM 5.3K Jun 6 20:31 scalac*
-rwx------+ 1 SYSTEM SYSTEM 5.3K Jun 6 20:31 scaladoc*
-rwx------+ 1 SYSTEM SYSTEM 5.3K Jun 6 20:31 scalap*
Command is working, but not in the for loop.
$ for f in $(ls | egrep -v .bat); do echo $f; done
fsc
fsc.bat
scala
scala.bat
scalac
scalac.bat
scaladoc
scaladoc.bat
scalap
scalap.bat
scalac
scalac.bat
scaladoc
scaladoc.bat
scalap
scalap.bat

No comments:

Post a Comment