Use printf to built a list of arguments and xargs to built command lines from it to work around that: Note that neither of these approaches work for lines containing whitespace Continue reading to see how the read command works through various examples. If you have whitespace or globbing characters in the lines use either xargs (see this answer) or parallel (see this answer). Marianne Guenot. You can use a bash command or script to streamline the process. Suppose we have another text file that contains the names of the months. How about saving the world? {} is the placeholder for the argument(s) and can be omitted if theres no other placeholder used in the command and the argument(s) should be added to the end of the command, which I did before. Jonathan Leffler Sep 6, 2011 at 2:50 34 This is a place In the photo, the shadow of the moon can be seen as it moves across the face of the Earth in the South Pacific. The Bash read command is a built-in utility that reads text from standard input. Copy or type the script above into an editor and save it with the filename script2.sh. Make it executable with chmod : Now we can run it and pass in a new data file, data3.txt. This has a list of the months in it, and one line with many words on it. You can simply use bash redirection and command substitution to get the file contents as arguments to your command: This works because not only space is a valid word splitting character, but newline as well you dont need to substitute anything. Add the -p tag and provide the prompt text, for example: The prompt text prints and requires user input. You can watch the whole thing live here at Space.com, via ispace (opens in new tab), starting at 11 a.m. EDT (1500 GMT). Read more Its pretty easy to read the contents of a Linux text file line by line in a shell scriptas long as you deal with some subtle gotchas. Concatenate multiple strings with spaces in them? Ron DeSantis for assailing Disney, which employees tens of thousands of their constituents, just to court MAGA voters. +1 anyway. Learn more about Stack Overflow the company, and our products. You can use a while loop with process substitution: An optimal way to read a multiline variable is to set a blank IFS variable and printf the variable in with a trailing newline: Note: As per shellcheck sc2031, the use of process substition is preferable to a pipe to avoid [subtly] creating an subshell. Using this trick, we can easily pass in the name of the data file that we want the script to work on. Just to complement DigitalRoss's response. For that case that you want to alter the IFS just for this command, you can use parenthesis. If you do I want to identify lines that contain only spaces. "bad array subscript" error when adding entry to an associative array, Bash - Problems creating array from a command output which has quoted text with spaces, for detects the spaces in the file as multiple lines. Specifying the argument names is optional. Site design / logo 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA. Dave McKay first used computers when punched paper tape was in vogue, and he has been programming ever since. Has depleted uranium been considered for radiation shielding in crewed spacecraft beyond LEO? How can I avoid that? In the body of our loop, were incrementing the Counter variable by one and using echo to send some output to the terminal window. Here's how it's done: for elem in "$ {files [@]}" From the bash manpage: Any element of an array may be referenced using $ {name [subscript]}. This site is protected by reCAPTCHA Enterprise and the Google Privacy Policy and Terms of Service apply. By clicking Accept all cookies, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
