For loops 1. Simple examples 1.1 Simple loop without needing to expand $ msh -c "for i in a b c; do echo hello; done" hello hello hello 1.2. Simple word list to echo $ msh -c "for foo in a b c; do echo \$foo; done" a b c 1.3 In conjunction with path globbing $ touch hello.txt world.txt $ sh -c "for foo in *.txt; do echo \$foo; done" hello.txt world.txt $ msh -c "for foo in *.txt; do echo \$foo; done" hello.txt world.txt 1.4 Subshells and stuff! $ cat >test.sh << EOF > echo hello > abc.md > echo world > def.md > for f in *.md; do > rev "\$f" > "\$(basename -- "\$f" .md).rev" > done > EOF $ msh test.sh $ ls abc.md abc.rev def.md def.rev hello.txt test.sh world.txt 1.5 Pipelines $ cat >test.sh << EOF > for i in "olleh" "dlrow"; do > echo \$i > done | rev > EOF $ sh test.sh hello world $ msh test.sh hello world 1.6 Redirects $ cat > test.sh << EOF > for i in a b; do > echo \$i > done > hello.txt > echo "The file contains:" > cat hello.txt > EOF $ sh test.sh The file contains: a b $ msh test.sh The file contains: a b 1.7 Good handling of the for-loop context. $ cat > test.sh << EOF > VALUES="a b c d" > ok="false" > > for v in \$VALUES; do > if [ \$v = "c" ]; then ok="true"; fi > done > > echo "ok is \$ok" > EOF $ sh test.sh ok is true $ msh test.sh ok is true 1.8 Handling of empty expansions $ cat > test.sh << EOF > SUITE="stable" > EXTRA_SUITES="" > for s in \$SUITE \$EXTRA_SUITES \$UNASSIGNED; do > echo "Downloading /foo/\$s/bar" > done > EOF $ sh test.sh Downloading /foo/stable/bar $ msh test.sh Downloading /foo/stable/bar