[ILUGC] Re: help to understand this bash output

  • From: Raman Pandarinathan <raamanp@xxxxxxxxx>
  • To: ilugc@xxxxxxxxxxxxx
  • Date: Mon, 16 Jan 2023 12:05:09 +0530

On Sun, Jan 15, 2023 at 12:33 AM M K Saravanan <mksarav@xxxxxxxxx> wrote:


Hi,
Can someone help me to understand this bash output:

$ echo '1 2 3 4 5 6' | while read a b c; do
echo result: $c $b $a;
done
result: 3 4 5 6 2 1

What logic is used to arrive at that answer?

1. read reads entire echo in one go.
2.No use of while in this case
3.When read reads a b c here is the assignment
a=1 b=2  and c='3 4 5 6'
Hence when you echo $c $b $a
You get (3 4 5 6) (2 ) (1)
You can understand shell execution better by using -x option.
Here is the output of the script saved and run with -x option

sh -x scriptfile
+ echo 1 2 3 4 5 6
+ read a b c
+ echo 1
1
+ echo 2
2
+ echo 3 4 5 6
3 4 5 6
+ read a b c

Raman.P
blog:http://ramanchennai.wordpress.com/
---
Mailing List Guidelines: https://ilugc.in/mailing-list-guidelines
Web: http://ilugc.in/
Internet Relay Chat: #ilugc on libera.chat

Other related posts: