directory/*
is expanded by the shell first (normally bash). Running echo directory/*
will echo all matching files, and bash will sort these matching elements lexicographically first, before passing them as arguments to echo
, which then prints them.
In the case of running ls directory/*
, the directory/*
is first expanded by the shell, and then presented as a list of arguments to ls
. The ordering which ls
then uses to output the files depends on the optional arguments supplied to it, which allow for outputting the file list based on alternative criteria, such as size, time modified etc, but this sorting is done internally within the ls
binary itself, after it has been passed the lexicographically sorted expanded argument list from the bash shell.