Bash and filenames with spaces.

Tuesday, June 24, 2008 22:09
Posted in category Linux

If you want to use Linux Bash scripts for doing file operations on files that have spaces in them, you will see that, by default, a ‘for’ statement will break up the filename in however many parts there are in the name.

E.g. “This is a file.txt” will become “This”, “is”, “a”, “file.txt”.

To prevent this from happenening, add this line to your code;

IFS=$'\n'

A working example:

#!/bin/sh
IFS=$'\n'
for f in `grep -Rl something /pathtofiles/*`
do replace 'hello' 'byebye' -- "$f"
done

You can leave a response, or trackback from your own site.
Tags: ,

Leave a Reply

You must be logged in to post a comment.