Bash and filenames with spaces.
Tuesday, June 24, 2008 22:09Posted in category Linux
No Comments
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 follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.
Leave a Reply
You must be logged in to post a comment.
