11

I learned that -r means recursive, which means the command can be performed in all subdirectories. To make sure I understand, I wrote the two functions below to make some tests. The file where I wrote the following code was named as test.sh. Inside the same directory I have a sub-directory called subtest. There's a file named xx.sh either in the current directory of in the sub-directory.

#!/bin/bash
function aa {
  if [ ! -r $1 ]
  then
    echo "not exist"
  else
    echo "exist"
  fi
}

function bb {
  if [ -r $1 ]
  then
    echo "exist"
  else
    echo "not exist"
  fi
}

aa xx.sh / bb xx.sh

The results I found:

For aa function, with -r, it will only print "exist" if xx.sh is in the current directory. It won't look for xx.sh in the sub-directory. Without -r, it will print "exist" when xx.sh is either in current directory or in the subdirectory.

For bb function, with -r, it will only print "exist" when the current directory has xx.sh. Without -r, it will always print "exist", even if there is no xx.sh at all (which surprised me and does not make sense!).

Can anyone explain why the results above happen and what does -r do exactly?

ruakh
  • 175,680
  • 26
  • 273
  • 307
Consideration
  • 182
  • 1
  • 1
  • 12
  • 3
    Maybe this should be closed as a duplicate of [how to look up these flags](https://stackoverflow.com/questions/19074542/list-of-if-switches-anywhere) – that other guy Dec 20 '19 at 20:23
  • This might help: https://explainshell.com/explain?cmd=if+%5B+%21+-r+%241+%5D%3B+then+echo%3B+fi and https://stackoverflow.com/a/26475397/3776858 – Cyrus Dec 20 '19 at 20:24
  • 2
    The `-r` tests if the file exists and if you have read permission on the file. [Bash scripting tutorial - if statement](https://ryanstutorials.net/bash-scripting-tutorial/bash-if-statements.php). – lurker Dec 20 '19 at 20:24
  • 2
    man test .... -r FILE FILE exists and read permission is granted – Red Cricket Dec 20 '19 at 20:24
  • 5
    The meaning of `-r` depends on what program/command it is given as an argument for. In this case it is for `[`, and means to check whether the file named by the next argument is readable. See `man [` or `man test`, as well as `help test`. – Arkku Dec 20 '19 at 20:24
  • 1
    Does this answer your question? [List of 'if' switches anywhere?](https://stackoverflow.com/questions/19074542/list-of-if-switches-anywhere) – Michael Piefel Dec 20 '19 at 20:28
  • 2
    Shell syntax and semantics are *extremely* context-dependent. `-r` can mean "recursive" (as an option to some file manipulation commands, although others use `-R` for this), "is there read access?" (to the `[` and `test` commands), "read only" (to the `mount` command), "read from here" (to `fdisk`), "reboot" (to `shutdown`), "resume" (`screen`), ... It can also have syntactic meaning in places other than command options, where it'll mean still different things. – Gordon Davisson Dec 20 '19 at 21:04

1 Answers1

23

My documentation for the conditional (and this is what you are looking for, since it is within [ and ]) reads:

 -r file
              True if file exists and is readable.
Michael Piefel
  • 18,660
  • 9
  • 81
  • 112