Macopare
Macopare

Reputation: 155

Read variable that has space character in its value

I have some paths stored in a variable (variable name -> test)

echo "${test}" 

Result:

/abc/pqr/filepath1
/abc/pqr/lmn/file path2
/abc/pqr/rst/filepath3

I want to escape "space" character in the second path and get the owner for each path, I'm using following command:

stat -c '%U' ${test}   , works for 1st and 3rd path.

How do I make this work for the 2nd path? Thanks in advance.

Upvotes: 1

Views: 550

Answers (1)

heemayl
heemayl

Reputation: 41987

Use a while construct to get each filename, one per line:

while IFS= read -r f; do stat -c '%U' "$f"; done <<<"$test"

Example:

$ echo "$test"
/abc/pqr/filepath1
/abc/pqr/lmn/file path2
/abc/pqr/rst/filepath3

$ while IFS= read -r f; do echo "$f"; done <<<"$test"
/abc/pqr/filepath1
/abc/pqr/lmn/file path2
/abc/pqr/rst/filepath3

Upvotes: 1

Related Questions