Well I've this code
dirname=
if [ -d $dirname ];
then
cd $dirname && rm *
fi
as you see I've this empty variable, what I want to know is why when using thing like this empty variable with the single square brackets it removes all the user's home directory files
And if I used the double square brackets it does not remove the user's home directory files
Like this
dirname=
if [[ -d $dirname ]];
then
cd $dirname && rm *
fi
I've read the difference syntax when using both Single Square Brackets and Double Square Brackets
May I know why this happens ?
$dirname
is empty, your test becomes[ -d ]
, which returns true if-d
is a non-null string (i.e. always true). See the related question How does bash interpret the equal operator with no surrounding spaces in a conditional? – steeldriver Jun 27 '20 at 19:03"$dirname"
(mind the quotes) is not a directory, thencd "$dirname"
will fail andrm *
will not be executed. – chepner Jun 28 '20 at 19:53cd
will follow symlinks while[ -d
will exclude them, so the above code makes a slight difference (although its most likely not intended by OP) ;) – alecxs Jun 28 '20 at 22:30