-3

I have code with two variables in echo. I don't know why it prints spaces before $NEXT even though I have just one space in code.

NEXT=$(find "${DIR}" -type f -name "*.$ext" | sed "s/.*\/\.//g" | sed "s/.*\///g" |
sed -n '/.*\..*/p' | wc -l)
echo "Files .$ext: $NEXT"

Files .tar:        1
Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
user3463055
  • 121
  • 2
  • 9

2 Answers2

1

Your find expression is not doing what you think it is:

NEXT=$(find "${DIR}" -type f -name "*.$ext" | sed "s/.*\/\.//g" | sed "s/.*\///g" |
sed -n '/.*\..*/p' | wc -l)

When you pipe to wc -l you are left with a Number. The format of the number will depend on your distributions default compile options for wc. While generally when information is piped or redirected to wc the value returned should be without any leading whitespace (but there is no guarantee that your install of wc will work that way). All you can do it test and see what results, e.g.

ls "$HOME" | wc -l

If whitespace is returned before the value -- you have found your problem.

David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
0

If the last line is the output, then it seems it is an output of something else than displayed code. When your output looks weird, try putting single quotes around each variable:

echo "  Average file size .'$ext': '$AEXT'"

That way, you will know, if the spaces (or tabs) are coming from the variables themselves or from the script.

Honza Remeš
  • 1,193
  • 8
  • 11