Reading a variable line by line with while loop

Smoot Carl-Mitchell smoot at tic.com
Tue Dec 1 15:17:37 UTC 2009


On Tue, 2009-12-01 at 06:57 -0800, Ray Parrish wrote:
> Ray Parrish wrote:
> >
> Hello again,
> 
> OK, I understand that the | is causing a sub shell to execute for the 
> echo command, so the variable $History even 'though declared globally, 
> is local to the loop with the pipe, and therefore not available after 
> the loop exits.
> 

Right.  subshells do not pass their environment back to their parent.
The read command is pretty inefficient.  You might want to learn sed.
It is well suited to this particular task.  Something like:

History=`sed 's/ /__/g' ~/.bash_history`

The substitution replaces all spaces in the Bash history with __.
Output is interpolated inline and assigned to the History variable.

-- 
Smoot Carl-Mitchell
Computer Systems and
Network Consultant
smoot at tic.com
+1 480 922 7313
cell: +1 602 421 9005




More information about the ubuntu-users mailing list