This is because you have files n
and t
in your working directory. ?
gets replaced by any files with one character.
$ ls
a t myfile
$ echo $string
question mark: a t
To solve it, double quote your variable when echo
ing:
$ echo "$string"
question mark: ?
In general, as devnull comments in Why does bash “echo [t]” result in “t” not “[t]”:
Shell Command Language tells that the following characters are special to the shell depending upon the context:
* ? [ # ~ = %
Moreover, following characters must be quoted if they are to represent themselves:
| & ; < > ( ) $ ` \ " ' <space> <tab> <newline>
So for example with *
you get all the files:
$ string='question mark: *'
$ echo $string
question mark: a myfile t