Rumen Hristov Rumen Hristov - 1 year ago 112
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:



Here's my code:


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

How can I fix my output? Thanks

Answer Source

I suggest

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

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download