I'm having trouble setting variables using the 'read' command in bash.
All of the following lines fail to set $var and return a blank line.
- echo "text" | read var ; echo $var
- cat file | read var ; echo $var
- read var < file | echo $var
I can get it work by explicitly declaring the fil
Kevan Gelling wrote:
I'm having trouble setting variables using the 'read' command in bash.
All of the following lines fail to set $var and return a blank line.
- echo "text" | read var ; echo $var
- cat file | read var ; echo $var
- read var < file | echo $var
The problem is that the pipelin
Kevan Gelling wrote:
> All of the following lines fail to set $var and return a blank line.
> - echo "text" | read var ; echo $var
> - cat file | read var ; echo $var
> - read var < file | echo $var
The other reply by Andy R is spot on, you can't modify the current
environment from a subshe
(Please excuse the unfinished last mail, I hit "Ctrl-Enter"
by accident.)
> - echo "text" | read var ; echo $var
> - cat file | read var ; echo $var
> - read var < file | echo $var
.. continuing from last time, all of these won't work
because you're always creating a new subshell.
Try
r
> Another related quirk, is that variables set within
> 'while read' loops lose
> their values once the loop ends. The following example
> displays "text text"
> within the loop and blank line outside.
> - echo "text" |\
> while read
> do
> foo=$REPLY ; bar="text"
> echo $foo
5 matches
Mail list logo