(Shell script) variables inside loop get reset.

Solution In Progress - Updated -

Issue

  • Running the following code:
files=0
directories=0
other=0
ls -A /some/directory | while read entry
do
   [ -f $entry ] && files=$((files+1)) && continue
   [ -d $entry ] && $=$((directories+1)) && continue
   other=$((other+1))
done
echo Files = $files.
echo Directories = $directories.
echo Other = $other

always results in:

Files = 0.
Directories = 0.
Other = 0.

no matter which direcory is used in place of "/some/directory" in the above example.

Environment

  • Red Hat Enterprise Linux (all)

  • ksh

  • bash

Subscriber exclusive content

A Red Hat subscription provides unlimited access to our knowledgebase of over 48,000 articles and solutions.

Current Customers and Partners

Log in for full access

Log In
Close

Welcome! Check out the Getting Started with Red Hat page for quick tours and guides for common tasks.