DEV Community

Discussion on: Two Bash scripts I cannot live without when working with Git

Collapse
 
erykpiast profile image
Eryk Napierała

You're perfectly right! Back then I had some issues with the "clean" solution and no idea how to do it better. It just worked, so I kept it like this :)

You made me feel a bit challenged, though, so I've decided I'll try to fix the issue.

Basically, the ideal solution would look like this:

TO_REMOVE=`git clean -f -d -n`;
if [[ `echo $TO_REMOVE | wc -l` -ne "0" ]]; then

For some reason, it doesn't work in the case when there is nothing to clean - line count is always equal or greater to one. I cannot remember it, but I suppose that was the exact issue five years ago when I was writing the script.

But now I'm smarter (or just know Bash a bit more :) ) and I know how to do it right! echo adds a new line to the end of the printed string, even when it's empty. Unless -n switch is specified! So the code below works perfectly in all cases.

TO_REMOVE=`git clean -f -d -n`;
if [[ `echo -n $TO_REMOVE | wc -l` -ne "0" ]]; then

I'll update the scripts in the article. Thank you!