Skip to content

Switch syntax for testing file presence#1138

Closed
eclipseo wants to merge 1 commit intoAcode-Foundation:mainfrom
eclipseo:patch-1
Closed

Switch syntax for testing file presence#1138
eclipseo wants to merge 1 commit intoAcode-Foundation:mainfrom
eclipseo:patch-1

Conversation

@eclipseo
Copy link

@eclipseo eclipseo commented Jan 7, 2025

[[ ]] is a bashism, adopted by some other shells (zsh, ksh) but not all. It does provide extra features (pattern matching and regex).

The POSIX way would be to use test or it's synonym [ ]. Both forms are equivalent, and the square brackets version [ ] requires spaces around the brackets as well as around each operator and operand. Here are some examples:

if [ -f "file.txt" ]; then echo "File exists"; fi

  • if test -f "file.txt"; then echo "File exists"; fi

The POSIX standard does not include the double brackets [[ ]], so for scripts that need to be portable across different POSIX-compliant shells, we should stick with [ ] or test.

[[ ]] is a bashism, adopted by some other shells (zsh, ksh) but not all. It does provide extra features (pattern matching and regex).

The POSIX way would be to use `test` or it's synonym ` [  ] `. 
Both forms are equivalent, and the square brackets version [ ] requires spaces around the brackets as well as around each operator and operand. Here are some examples:

 ‐ `if [ -f "file.txt" ]; then echo "File exists"; fi`
 - `if test -f "file.txt"; then echo "File exists"; fi`

The POSIX standard does not include the double brackets [[ ]], so for scripts that need to be portable across different POSIX-compliant shells, we should stick with [ ] or test.
@eclipseo
Copy link
Author

eclipseo commented Jan 7, 2025

Note that I don't have the means to test this, I don't have a build environment for Android and so on.

@bajrangCoder
Copy link
Member

This isn't the solution, instead we can use test -f filename

@bajrangCoder
Copy link
Member

#1167 will fix this issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants