Friday, October 9, 2020

Bash : empty variable value outside of while loop using pipelines for input

Instead of the following which returns empty value of a :

$> a=0 ; find /dev/shm/ -uid 54321 -ls | awk '{print $7}' | while read line ; do let a+=${line}; done ; echo "a="${a}

try to use bash's process substitution like this :

a=0 ; while read line ; do let a+=$line ; done < <(find /dev/shm/ -uid 54321 -ls | awk '{print $7}') ; echo "a="$a

This will prevent using newly created shells for pipelines, using the current shell for all operands. Good Luck !

No comments:

Post a Comment