bash - using "wc -l" on script counts more than using on terminal -


i'm making bash script , it's this:

#!/bin/bash dnum=$(ls -lar / 2> /dev/null | grep '^d' | wc -l) echo there $dnum directories. 

the problem is, when run line directly on terminal:

ls -lar / 2> /dev/null | grep '^d' | wc -l 

i number.

but when run script displays me greater number, 30 50 more.

what problem here?
why "wc" command counting more lines when running script?

you may have different directory roots 2 runs. instead of ls find directories can use this

find parent_directory -type d 

and pipe wc -l count.

the /proc directory have processes , treated directories , change run run. exclude count use

find / -path /proc -prune -o -type d | wc -l 

Comments