Technical article 116444

Why does "echo ?" return the filename(s) if the filename is single character?

OpenServer 5.0.5OpenServer 5.0.6OpenServer 5.0.7UnixWare 7.1.0UnixWare 7.1.1UnixWare 7.1.3UnixWare 7.1.4Open UNIX 8

Written · Last revised

Problem

When I run the command "echo ?" then "?" is returned.  However,
if I create a filename consisting of one character in the filename
and run the same command it doesn't return "?", it returns the
name of the file I just created.
For example:
# mkdir /tmp/empty
# cd /tmp/empty
# echo ?
?
# > ab
echo ?
?
# > a
echo ?
a
# > b
echo ?
a b

Cause

          If a command word includes the "*", "?",  or "[" characters,
	  and the -f flag has not been set, the shell regards the word
          as a pattern.  The shell replaces the word with file names,
          sorted according to the collating sequence in effect in the
          current locale, that match that pattern.  If the shell does
          not find a file name to match the pattern, it does not change
          the word.

When the shell uses a pattern for file name substitution, the "." and "/" characters must be matched explicitly.

"*" Matches any string, including the null string.

"?" Matches any single character.

"echo ?" Matches all files of a single character.

"echo ??" Matches all files of two characters.

"echo *" Matches all files, regardless of length.

"echo [Hello]" Matches any file of a single character if that character is "H", "e", "l", or "o".

The basic premise is that a "?" is treated by the shell as a pattern and will match any single character filename as long as one exists. If there is no filename match, then it will return the "?".

Therefore, unlike DOS in which each program has to expand regular expressions ("*" and "?") within the command line, UNIX shells perform the expansion prior to passing the arguments to the command itself.

	  This means that:
# ls ?
and
# echo ?

will be both expanded in the same way to:

# ls a b
# echo a b

Solution

If you want only "?" displayed, ensure that the "?" is in quotes:

Example:
# echo "?"

See also

http://www.rs6000.ibm.com/idd500/usr/share/man/info/en_US/a_doc_lib/aixuser/usrosdev/parameter_subst.htm

regexp(5) man page on UnixWare/Open UNIX 8 regexp(S) on OpenServer, regex(7) on OpenLinux,

The man page for the shell used.

From the Xinuos technical article database, record 116444. Reproduced with permission. The original is at www.sco.com/ta/116444, which is reachable but blocked from search engines.

Next step

Need this fixed rather than explained?

Tell us the release, what the system runs and what it is doing. We will reply by email with what we would do next.

  • You deal with the people who do the work
  • No obligation, and no upgrade required before we will help
  • We tell you plainly if something is not worth doing

Send an enquiry

We reply by email. If a server is down, say so in the first line. You can also write to help@scoexpert.com.