Pages

Monday, 28 October 2013

Bash Loops and the Dreaded Space

The for loop in Bash uses the environment variable IFS to work out what is a different item in a for loop. By default this is all manor of white space. Sometimes (like when working with files) it is useful to change this to be just newlines. Example below.


#!/bin/bash
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
for i in *
do
  echo $i

done
IFS=$SAVEIFS



No comments:

Post a Comment