Quantcast
Channel: How to read complete line in 'for' loop with spaces - Ask Ubuntu
Browsing latest articles
Browse All 8 View Live

Answer by bac0n for How to read complete line in 'for' loop with spaces

Mapfile is a convenient way to read lines from a file into an indexed array, not as portable as read but slightly faster. By using for loop you avoid creating a subshell. #!/bin/bash mapfile -t <...

View Article



Answer by odoncaoa for How to read complete line in 'for' loop with spaces

Dandalf got real close to a functional solution, but one should NOT EVER be trying to assign the result of unknown amounts of input (i.e. find ~/.gdfuse -name '*') to variables! Or, at least be trying...

View Article

Answer by mik for How to read complete line in 'for' loop with spaces

I would write it like this: cat ./file_wget_med | while read -r j do echo $j done as it requires least changes in the original script (except for the solution using IFS, but it modifies bash behavior...

View Article

Answer by Dandalf for How to read complete line in 'for' loop with spaces

Here is a slight extension to Mitchell Currie's answer, that I like because of the small scope of side effects, which avoids having to set a variable: #!/bin/bash while read -r fname; do echo $fname...

View Article

Answer by Mitchell Currie for How to read complete line in 'for' loop with...

#!/bin/bash files=`find <subdir> -name '*'` while read -r fname; do echo $fname done <<< "$files" Verified working, not the one liner you probably want but it's not possible to do so...

View Article


Answer by Radu Rădeanu for How to read complete line in 'for' loop with spaces

for loop splits when it sees any whitespace like space, tab, or newline. So, you should use IFS (Internal Field Separator): IFS=$'\n' # make newlines the only separator for j in $(cat ./file_wget_med)...

View Article

Answer by evilsoup for How to read complete line in 'for' loop with spaces

for loops split on any whitespace (space, tab, newline) by default; the easiest way to work on one line at a time is to use a while read loop instead, which splits on newlines: while read i; do echo...

View Article

How to read complete line in 'for' loop with spaces

I am trying to run a for loop for file and I want to display whole line. But instead its displaying last word only. I want the complete line. for j in `cat ./file_wget_med` do echo $j done result...

View Article

Browsing latest articles
Browse All 8 View Live




Latest Images