diff --git a/doc_src/cmds/set.rst b/doc_src/cmds/set.rst index 8101e60d1..446a19a45 100644 --- a/doc_src/cmds/set.rst +++ b/doc_src/cmds/set.rst @@ -59,7 +59,7 @@ The following other options are available: - ``-e`` or ``--erase`` causes the specified shell variables to be erased -- ``-q`` or ``--query`` test if the specified variable names are defined. Does not output anything, but the builtins exit status is the number of variables specified that were not defined, or 255 if more than 255 variables are not defined. +- ``-q`` or ``--query`` test if the specified variable names are defined. Does not output anything, but the builtins exit status is the number of variables specified that were not defined, up to a maximum of 255. If no variable was given, it also returns 255. - ``-n`` or ``--names``: List only the names of all defined variables, not their value. The names are guaranteed to be sorted. diff --git a/src/builtin_set.cpp b/src/builtin_set.cpp index 286cd29dd..7d256daee 100644 --- a/src/builtin_set.cpp +++ b/src/builtin_set.cpp @@ -466,6 +466,10 @@ static int builtin_set_query(const wchar_t *cmd, set_cmd_opts_t &opts, int argc, int retval = 0; env_mode_flags_t scope = compute_scope(opts); + // No variables given, this is an error. + // 255 is the maximum return code we allow. + if (argc == 0) return 255; + for (int i = 0; i < argc; i++) { auto split = split_var_and_indexes(argv[i], scope, parser.vars(), streams); if (!split) { diff --git a/tests/checks/set.fish b/tests/checks/set.fish index 2d448519f..08681beb9 100644 --- a/tests/checks/set.fish +++ b/tests/checks/set.fish @@ -820,3 +820,11 @@ function erase-funcvar end erase-funcvar + +set --query $this_is_not_set +echo $status +# CHECK: 255 + +set --query +echo $status +# CHECK: 255