George S.
George S.

Reputation: 49

BASH: How to add text in the same line after command

I have to print number of folders in my directory, so i use ls -l $1| grep "^d" | wc -l after that, I would liked to add a text in the same line. any ideas?

Upvotes: 1

Views: 2510

Answers (2)

Mike Wodarczyk
Mike Wodarczyk

Reputation: 1273

If you don’t want to use a variable to hold the output you can use echo and put your command in $( ) on that echo line.

echo $(ls -l $1| grep "^d" | wc -l ) more text to follow here

Upvotes: 1

Barmar
Barmar

Reputation: 782498

Assign the result to a variable, then print the variable on the same line as the directory name.

folders=$(ls -l "$1" | grep "^d" | wc -l)
printf "%s %d\n" "$1" "$folders"

Also, remember to quote your variables, otherwise your script won't work when filenames contain whitespace.

Upvotes: 1

Related Questions