I guess this will handle all cases:
find . -maxdepth 1 -type d -exec echo \; | wc -l
For each dir I print an empty newline... then count the newlines. Sadly wc
does not work on null terminated strings, but we could remove all except zeros and count them:
find . -maxdepth 1 -type d -print0 | tr -cd '\0' | wc -c
As to your script, you are getting the error, because you need to enclose to comment in $( .. )
if you want to save it's output into a variable. Bash is space aware, I mean a=1
is assigment, and a = 1
is run program named a
with arguments =
and 1
. Bash interprets the line: var=1 bash -c 'echo $var'
as first it sets var=1
then runs the program bash
with arguments -c
and 'echo $var'
. Try this:
assetid=$(ls -l /home/user/Desktop/folder | grep -c "^d")
But don't parse ls
output. ls
is for human readable nice colored output, it's better to prefer using different utilities in batch/piped scripts.