Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash read command doesn't work outside loop

Tags:

bash

shell

input

I must be missing something very fundamental about the Bash read command. At the shell prompt, this fails to assign the three input fields to the corresponding variables:

% echo a b c | read x1 x2 x3
% echo $x1 $x2 $x3

% 

This works though:

% echo a b c | while read x1 x2 x3 ; do
> echo $x1 $x2 $x3
> done
a b c
%

I only have to read one line of input. Using a while loop is incorrect here, as I lose the values of x1, x2, and x3 when the loop's subprocess exits. I'd have to place all the code that accesses them within the loop body (and perhaps break at the end, for "clarity"), which seems very hackish. Using it within an if command works as well, but suffers from the same subprocess issues as a while loop:

% echo a b c | if read x1 x2 x3 ; then echo $x1 $x2 $x3; fi
a b c
% echo $x1

BASH_VERSION reports as "4.2.45(1)-release".

like image 394
smontanaro Avatar asked Sep 27 '13 13:09

smontanaro


2 Answers

The problem is the pipe. From bash manual:

Each command in a pipeline is executed as a separate process (i.e., in a subshell).

You have to do reverse, i.e., call read in current process and have input generated in subprocess. For example:

$ read x1 x2 x3 < <(echo a b c)
$ echo $x1 $x2 $x3
a b c
like image 133
danadam Avatar answered Oct 13 '22 07:10

danadam


read is a bash built-in command which is being run in a bash subprocess. It is setting the environmental variables within that subprocess, so when you check the values in the original process, nothing has been changed.

like image 28
Gavin Smith Avatar answered Oct 13 '22 07:10

Gavin Smith