diff --git a/builtin_test.cpp b/builtin_test.cpp
index 1cf798960..2fd3f916d 100644
--- a/builtin_test.cpp
+++ b/builtin_test.cpp
@@ -33,13 +33,15 @@ enum token_t
test_bang, // "!", inverts sense
test_filetype_b, // "-b", for block special files
- test_filetype_c, // "-c" for character special files
- test_filetype_d, // "-d" for directories
- test_filetype_e, // "-e" for files that exist
- test_filetype_f, // "-f" for for regular files
- test_filetype_g, // "-g" for set-group-id
- test_filetype_h, // "-h" for symbolic links
+ test_filetype_c, // "-c", for character special files
+ test_filetype_d, // "-d", for directories
+ test_filetype_e, // "-e", for files that exist
+ test_filetype_f, // "-f", for for regular files
+ test_filetype_G, // "-G", for check effective group id
+ test_filetype_g, // "-g", for set-group-id
+ test_filetype_h, // "-h", for symbolic links
test_filetype_L, // "-L", same as -h
+ test_filetype_O, // "-O", for check effective user id
test_filetype_p, // "-p", for FIFO
test_filetype_S, // "-S", socket
@@ -95,9 +97,11 @@ static const struct token_info_t
{test_filetype_d, L"-d", UNARY_PRIMARY},
{test_filetype_e, L"-e", UNARY_PRIMARY},
{test_filetype_f, L"-f", UNARY_PRIMARY},
+ {test_filetype_G, L"-G", UNARY_PRIMARY},
{test_filetype_g, L"-g", UNARY_PRIMARY},
{test_filetype_h, L"-h", UNARY_PRIMARY},
{test_filetype_L, L"-L", UNARY_PRIMARY},
+ {test_filetype_O, L"-O", UNARY_PRIMARY},
{test_filetype_p, L"-p", UNARY_PRIMARY},
{test_filetype_S, L"-S", UNARY_PRIMARY},
{test_filesize_s, L"-s", UNARY_PRIMARY},
@@ -812,25 +816,31 @@ static bool unary_primary_evaluate(test_expressions::token_t token, const wcstri
case test_filetype_b: // "-b", for block special files
return !wstat(arg, &buf) && S_ISBLK(buf.st_mode);
- case test_filetype_c: // "-c" for character special files
+ case test_filetype_c: // "-c", for character special files
return !wstat(arg, &buf) && S_ISCHR(buf.st_mode);
- case test_filetype_d: // "-d" for directories
+ case test_filetype_d: // "-d", for directories
return !wstat(arg, &buf) && S_ISDIR(buf.st_mode);
- case test_filetype_e: // "-e" for files that exist
+ case test_filetype_e: // "-e", for files that exist
return !wstat(arg, &buf);
- case test_filetype_f: // "-f" for for regular files
+ case test_filetype_f: // "-f", for for regular files
return !wstat(arg, &buf) && S_ISREG(buf.st_mode);
- case test_filetype_g: // "-g" for set-group-id
+ case test_filetype_G: // "-G", for check effective group id
+ return !lwstat(arg, &buf) && getegid() == buf.st_gid;
+
+ case test_filetype_g: // "-g", for set-group-id
return !wstat(arg, &buf) && (S_ISGID & buf.st_mode);
- case test_filetype_h: // "-h" for symbolic links
+ case test_filetype_h: // "-h", for symbolic links
case test_filetype_L: // "-L", same as -h
return !lwstat(arg, &buf) && S_ISLNK(buf.st_mode);
+ case test_filetype_O: // "-O", for check effective user id
+ return !lwstat(arg, &buf) && geteuid() == buf.st_uid;
+
case test_filetype_p: // "-p", for FIFO
return !wstat(arg, &buf) && S_ISFIFO(buf.st_mode);
diff --git a/doc_src/test.txt b/doc_src/test.txt
index fc64118e7..035671325 100644
--- a/doc_src/test.txt
+++ b/doc_src/test.txt
@@ -16,7 +16,9 @@ The following operators are available to examine files and directories:
- -e FILE returns true if \c FILE exists.
- -f FILE returns true if \c FILE is a regular file.
- -g FILE returns true if \c FILE has the set-group-ID bit set.
+- -G FILE returns true if \c FILE exists and its group matches the effective group id of this process.
- -L FILE returns true if \c FILE is a symbolic link.
+- -O FILE returns true if \c FILE exists and its owner matches the effective user id of this process.
- -p FILE returns true if \c FILE is a named pipe.
- -r FILE returns true if \c FILE is marked as readable.
- -s FILE returns true if the size of \c FILE is greater than zero.