Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

concatenate grep output to an echo statement in UNIX

Tags:

grep

shell

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

like image 954
Rumen Hristov Avatar asked Oct 15 '25 18:10

Rumen Hristov


1 Answers

I suggest

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

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

like image 142
Jens Avatar answered Oct 18 '25 12:10

Jens