How do I test if a file does not exist using ZSH?

17,717

Thanks to don_crissti for answering this for me.

The correct if block is below.

if [[ ! -a ~/.zkbd/$TERM-${${DISPLAY:t}:-$VENDOR-$OSTYPE} ]]; then
    zkbd
fi
Share:
17,717

Related videos on Youtube

Dave F
Author by

Dave F

Updated on September 18, 2022

Comments

  • Dave F
    Dave F almost 2 years

    This question is about ZSH, not bash.

    I have the following lines in my .zshrc file. Whenever I open a terminal I get a no matches found error referencing the line with the if statement.

    if [[!( -a ~/.zkbd/$TERM-${${DISPLAY:t}:-$VENDOR-$OSTYPE} )]]; then
        zkbd
    fi
    

    I read through the ZSH documentation and my if statement appears to be correct. I don't understand why I'm getting the error.

    What I'd like to happen is for the zkbd utility to run if the file in the .zkbd folder does not exist.

    • Stéphane Chazelas
      Stéphane Chazelas almost 9 years
      @Gilles, how is that a duplicate? OK, in both cases, adding spaces fixes the problem, but considering as duplicates any question where the solution is to add spaces somewhere sounds wrong to me.
    • Gilles 'SO- stop being evil'
      Gilles 'SO- stop being evil' almost 9 years
      @StéphaneChazelas How are they not duplicates? Both are asking why [ … ] doesn't work without spaces inside.
    • Stéphane Chazelas
      Stéphane Chazelas almost 9 years
      @Gilles, one is about the [ command, the other one about the [[...]] construct, one is about bash, the other one is about zsh. The error messages are completely different (at least it would make sense to explain why you get a no-match error in zsh here).
    • Stéphane Chazelas
      Stéphane Chazelas almost 9 years
      @Gilles, also note that all of bash, ksh93 and mksh do support [[(a == b)]] (not [[!(a == b)]]), not zsh.
  • azorius
    azorius over 7 years
    for ADHD people like me, the solution is [[ -a FILE_NAME ]], the "! -a" asks if the file does not exist.
  • Dave F
    Dave F about 7 years
    @azorius My initial question was asking how to test if a file does not exist.
  • Keshav
    Keshav about 2 years
    can I ask what function this is? How do I search for [[ ... ]] in the manual?