Search code examples
bashunixposixdash-shell

echo prints too many spaces


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

Solution

  • 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.