Jump to content

For loop at the cli

From Squishu Wiki

General

  • Here is an example for loop at the cli
  • The first prompt is a $, but for each subsequent line the prompt is a >
for file in *
do
if grep -l blah $file
then
more $file
fi
done
  • the variable file could just as easily be called i, but file is self documenting
  • The grep prints the files it finds in the current directory containing blah, and then more prints the contents of the files
  • The * here is an example of wildcard expansion / globbing