Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch syntax for testing file presence #1138

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

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
Collaborator

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

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