Search code examples
bashshellsyntaxglob

Bash - meaning of a simple question mark (?)


I was playing around with some bash features and as I tried echo-ing some output, I noticed, that

echo what about in some more complex example ?

results in

what about in some more complex example \

I know that escaping the question mark or the whole line with quotes resolves the problem, but I am curious of why is it happening.

So my 2 questions are:

  1. What is the meaning of a simple question mark in bash (I know for example about the '$?' special parameter and regex usage) ?
  2. I suppose that is is a bash environment variable or some king of variable. How can I inspect a variable ? For example a command can be inspected with type keyword, i.e. type cd

Solution

  • In that context it functions as a glob pattern. If there are files with one-character names in the current working directory, the shell expands an unquoted question mark to their names.

    $ echo ? \? '?' "?"
    ? ? ? ?
    $ touch a b c
    $ echo ? \? '?' "?"
    a b c ? ? ?
    

    Similarly, ?? is expanded to two-character filenames, ??* to filenames longer than one character, and ??[ab] to three-character filenames ending with an a or a b, etc.

    See Filename Expansion for further information.