Bill Trok
Bill Trok

Reputation: 147

Why does the value of '?' change depending on my current directory?

I have this strange folder on my computer called template/ (I believe it was created when I was doing some low level programming on the raspberry pi). The strange thing is if I am in the folder and I run the command

$echo -n ? | hexdump

it displays

0000000 000d                                   
0000001

However, if I am in any other folder the command looks like this

$echo -n ? | hexdump
0000000 003f                                   
0000001

Why does the value of '?' change in that folder and only that folder.

I should note that the reason I found this was that there was a file named '?' in the template/ folder that was causing problems in some php I was writing. I attempted to recreate it somewhere else but found I was unable to.

Upvotes: 1

Views: 48

Answers (2)

TrueY
TrueY

Reputation: 7610

The expression echo ? is processed by first. It replaces ? with any single character file names if such exists. If there is no such file it remains ?.

Example (there are two files with single character file name):

$ ls ?
b  c
$ echo ?
b c

Or (no single character file)

$ ls ?
ls: cannot access ?: No such file or directory
$ echo ?
?

So if You just want to present a ? character you need to escape it:

$ echo \?
?
$ echo '?'
?
$ echo "?"
?

I hope this helps a bit...

Upvotes: 1

anubhava
anubhava

Reputation: 785621

Why does the value of '?' change in that folder and only that folder.

? is a special character in shell globbing.

? matches any character and echo ? will match and list any filename in current directory with exactly 1 character length.

PS: If a directory doesn't have any 1 character filename then it will echo literal ?

Upvotes: 4

Related Questions