How shell interprets "^"?

ls [^d]*

Why the above working like

ls *

There is no file named like [^d]. I was expecting shell to interpret it literally.

ls [^d]*

means to display all files which is not having the name starting from alphabet d.

Vikram, ^ is a character of regular expression set used by grep and sed.
Run the command and you see that it doesn't work the way you said.
ls has nothing to do with ^.
I think it's shell that is interpreting in some way but how?

Hello Ravi,

Create a file with a name like dov.txt or anything starting with d and then run the below command.

ls [^d]*

I bet it will not show you the file name starting with d i.e dov.txt.It is not neccesary to use it only with sed/grep.

[^ ] Matches a single character that is not contained within the brackets. For example, [^abc] matches any character other than "a", "b", or "c". [^a-z] matches any single character that is not a lowercase letter from "a" to "z". Likewise, literal characters and ranges can be mixed.

Actually it has nothing to do with sed or grep. It has to do with file globbing. Globbing is implemented by your shell and varies slightly from shell to shell. For example, in the case of Bash, you can control globbing functionality using the shopt -s extglob option.

Yup Fpmurphy is right. For more info on it Please check the below link.

Globbing

And the normal negation operator for standard globbing is ! not ^ (even though some shells also support ^ ), so it would have been better to use:

ls [!d]*
1 Like

FYI blast from the past
In older shells the circumflex was used before the pipe symbol to join STDOUT of one command to STDIN of another:

$ sh
$ who^wc -l
       8
$