Walt Mankowski via plug on 26 Apr 2023 13:10:49 -0700 |
[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]
Re: [PLUG] Splitting strings in bash |
Thanks! I was suspecting something like that. It makes sense after I thought about it for a bit. On Wed, Apr 26, 2023 at 03:57:43PM -0400, Carlos M. Fernández via plug wrote: > The first example with the pipe puts the read command in a subshell, and > that environment gets wiped out after the line is executed, taking the > updated variable values with it. The one with the here string doesn't, > allowing the read to modify the environment in the script. > > > On Wed, Apr 26, 2023, 15:46 Walt Mankowski via plug < > plug@lists.phillylinux.org> wrote: > > > Hi all, > > > > I've got a question about splitting strings in a bash shell > > script. Let's say I've got a string > > > > STR='dog cat' > > > > I'd like to split that on the space into two variables, foo and bar. I > > tried this > > > > echo $STR | read foo bar > > > > but it doesn't work. On the following line foo and bar are back to > > their original values. Then, after looking through the bash manpage > > for an alternative, I tried this instead > > > > read foo bar <<< $STR > > > > That DID work! > > > > So that solves my problem, but I'm still confused. Those two commands > > seem to me like they should be identical. Why doesn't the first one work? > > > > Walt > > ___________________________________________________________________________ > > Philadelphia Linux Users Group -- > > http://www.phillylinux.org > > Announcements - > > http://lists.phillylinux.org/mailman/listinfo/plug-announce > > General Discussion -- > > http://lists.phillylinux.org/mailman/listinfo/plug > > > ___________________________________________________________________________ > Philadelphia Linux Users Group -- http://www.phillylinux.org > Announcements - http://lists.phillylinux.org/mailman/listinfo/plug-announce > General Discussion -- http://lists.phillylinux.org/mailman/listinfo/plug ___________________________________________________________________________ Philadelphia Linux Users Group -- http://www.phillylinux.org Announcements - http://lists.phillylinux.org/mailman/listinfo/plug-announce General Discussion -- http://lists.phillylinux.org/mailman/listinfo/plug