Always include your file name, the idea of ββusing %q to escape spaces is correct, but when used with the [ unspecified $TFILE breaks into a few words, causing the -f operand to get too many arguments when it really expects one argument. Therefore, when you specify it twice, white spaces are preserved and a literal single argument is passed in the conditional expression.
testFile="abc def ghi" printf -v quotedFile '%q' "$testFile" if [ -f "$quotedFile" ]; then printf 'My quoted file %s exists\n' "$quotedFile" fi
the above should be well applied (using [ ) in any POSIX compatible shells. But if you target scripts only for the bash , you can use [[ , in which quoting is never required since it is evaluated as an expression. Therefore you can just do
file_with_spaces="abc def ghi" if [[ -f $file_with_spaces ]]; then printf 'My quoted file %s exists\n' "$file_with_spaces" fi
But in general, it doesn't hurt to add quotes to variables in bash . You can always do this.
source share