rtom
rtom

Reputation: 209

Echo expression in bash

I have been trying to print maximal depth of directory but I can't figure out how to print the results in the format "Maximal depth: xxx"

    dir_depth() {
      cd "$1"
      maxdepth=0
      for d in */.; do
        [ -d "$d" ] || continue
        depth=`dir_depth "$d"`
        maxdepth=$(($depth > $maxdepth ? $depth : $maxdepth))
      done
      echo $((1 + $maxdepth)) #this line is problem
    }

dir_depth "$@"

If I try to do it like

    foo=$((1 + $maxdepth))
    echo "Maximal depth: " $foo

then I get an error

Max depth: 1: expression recursion level exceeded (error token is "depth: 1")

Upvotes: 1

Views: 10439

Answers (1)

AlG
AlG

Reputation: 15157

You have all the parts, just need to assemble them correctly;

echo "Maximal depth:  $((1 + maxdepth))"

For example:

x=5
echo "Max $((1+x))"

Results in Max 6 being printed.

Upvotes: 2

Related Questions