* Home :: Get a random password :: Get a multi-word password :: What is my IP? (v4 | v6)
* My projects

Dealing with multi-line Bash variable values

Tags: quicktip,howto,bash,shell

Date: 20220627


Problem: getting a "multi-line" and "multi-word" (name and emails in this case) Bash variable value in shape to be used in a shell script loop:

# The variable
~]$ lalas="lala1 <lala@lala1>
lala2 <lala@lala2>
lala3 <lala@lala3>"

# Ouch #1 - well, that's not going to work
~]$ for i in $lalas;do printf $i; done
lala1<lala@lala1>lala2<lala@lala2>lala3<lala@lala3>
        
# Ouch #2 - this is even worse
~]$ for i in $lalas;do echo $i; done
lala1
<lala@lala1>
lala2
<lala@lala2>
lala3
<lala@lala3>
                
# Possible solution, treat it as an array
~]$ printf '%s\n' "${lalas[@]}"
lala1 <lala@lala1>
lala2 <lala@lala2>
lala3 <lala@lala3>
                
    


(c)The Nux(tm) ;-)

No CSS, no Javascript, no some HTML.
No ads, no tracking, no cookies.