I have a script variable which is multi-line.
How do i traverse this variable to read it line by line and process each line the way I want?
I have a script variable which is multi-line. How do i traverse this variable
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Consider the following multi-line variable
and a simple process for each line: just
echoit with a prefix=LINE:and with single quotes around the line. Either of the following codes will satisfy that requirement:or
Neither creates a subshell (so you can, e.g., set variables in the loop and access them outside of it), and both output
But suppose instead you have
and that leading and trailing whitespace matter for your application (e.g., parsing Git porcelain). Both the above codes will give exactly the same output for the latter variable/data as for the former, which is not what you want. To preserve leading and trailing whitespace, replace
while read linewithwhile IFS= read -r line. I.e., either of the following codesor
will produce
See Greg Wooledge’s excellent Bash FAQ for details.