Strangeness with bash not an operator

I am trying to determine if there is a file or directory. I tried the following commands to see if the file exists and they work correctly:

if [ -a 'settings.py' ]; then echo 'exists'; fi
exists #output
if [ -a 'foo' ]; then echo 'exists'; fi #outputs nothing

But when I try this:

if [ ! -a 'settings.py' ]; then echo 'does not exist'; fi
does not exist #shouldn't be output since the file does exist
if [ ! -a 'foo' ]; then echo 'does not exist'; fi
does not exist

'does not exist' is displayed no matter what.

+3
source share
3 answers

Try using -einstead-a

This page on the Linux Documentation Project says:

-aessentially the same as -e. But it is outdated, and its use is not recommended.

+1
source

[. [[, , , "gotchas", .

[[, :

pax> touch qq
pax> if [[ -a qq ]] ; then echo exists ; fi
exists
pax> if [[ ! -a qq ]] ; then echo not exists ; fi
pax> rm qq
pax> if [[ ! -a qq ]] ; then echo not exists ; fi
not exists
+2

you can use test

test -e "$file" && echo "exists"
0
source

Source: https://habr.com/ru/post/1766894/


All Articles