Will via plug on 26 Apr 2023 13:04:14 -0700


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [PLUG] Splitting strings in bash


aws '{print $2}' if you want the right piece. That's all I got. 

On Wed, Apr 26, 2023 at 3:58 PM Carlos M. Fernández via plug <plug@lists.phillylinux.org> 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