Rumen Hristov Rumen Hristov - 4 months ago 24
Bash Question

concatenate grep output to an echo statement in UNIX

I am trying to output the number of directories in a given path on a SINGLE line. My desire is to output this:


X-many directories


Currently, with my bash sript, I get this:


X-many

directories


Here's my code:

ARGUMENT=$1

ls -l $ARGUMENT | egrep -c '^drwx'; echo -n "directories"


How can I fix my output? Thanks

Answer

I suggest

 echo "$(ls -l "$ARGUMENT" | egrep -c '^drwx') directories"

This uses the shell's feature of final newline removal for command substitution.

Comments