For loop files with spaces
Collectives on Stack Overflow. Learn more. Asked 10 years, 9 months ago. Active 5 months ago. Viewed 79k times. Improve this question. Ross Ridge Andrew Bullock Andrew Bullock 35k 33 33 gold badges silver badges bronze badges. Try not to use parameter extensions letters for variables, if you later need to add them the code will be hard to read and prone to errors. The easiest and safest way is to use upper-case letters i. I hope this tip on handling spaces with the input field separator in your Linux shell scripts and for loops has been helpful.
Handling spaces in Linux shell script input and for loops. By Alvin Alexander. Last updated: April 8, I think the for loop's arguments should be expanded. But the command argument must not be expanded again. Also, the ls output seems pointless if nothing in the loop reads from stdin. Join Date: Aug The pipe feeding the loop does nothing useful because the filenames are picked by matching the pattern you give after in with the content of your current directory. Arguments for commands and programs are usually seperated by spaces.
Your initial attempt fails because unzip treats every part of the filename that is seperated by space as a filename. You have to quote the variable representing the filename to tell unzip , that it is indeed a single filename: Code :. Join Date: Feb I agree with MadeInGermany with one caveat: You are being precise with the number of characters in the date segments of the filename, and then blowing it with the spaces.
Your pattern would match this: Code :. Join Date: Jun Maybe I'm being thick, but what is the ls -lrt for? I don't see it having any purpose the way it is coded.
If you want to get the files matching the pattern in modified date order oldest first then would you be better with something more like this? Find all posts by rbatte1. Shell Programming and Scripting. Need Help on For Loop to pass space separated value as one value. They pose special problems and need to be dealt with in different ways. In this Linux quick tip we will discuss using a for loop on a file with spaces in the lines.
Now we want to use a for loop to iterate through this file and use each line as input for some action. When we create our for loop, the shell sees the spaces on the first line and assumes each is a different iteration. The IFS internal field separator controls this behavior. The shell uses the IFS for word splitting.
It defines the character or character set used as a delimiter when splitting words, or splitting lines into words. So when then the shell sees the spaces in the first line, it divides the line into four tokens four words.
0コメント