Home > Net >  Why does $1*$2 display filenames of everything in its directory?
Why does $1*$2 display filenames of everything in its directory?

Time:06-26

This is something I stumbled on when I made a mistake in my code (forgot $(()) lol), so I've fixed my silly mistake already, but why does $1*$2 give you a list of files and folders in the directory that the script is in?

Here is an example:

script.sh:

echo $1*$2
$ bash script.sh
random.bmp script.sh some text document.odt test file.txt test folder

The same thing happens if the command is just directly issued:

$ echo $1*$2
random.bmp script.sh some text document.odt test file.txt test folder

Does anyone know why it acts like this? Is there any documentation anywhere on the function? I briefly looked around about this but didn't find anyone else confused about it.

CodePudding user response:

Globs

"Glob" is the common name for a set of Bash features that match or expand specific types of patterns. Some synonyms for globbing (depending on the context in which it appears) are pattern matching, pattern expansion, filename expansion, and so on. A glob may look like *.txt and, when used to match filenames, is sometimes called a "wildcard".

Traditional shell globs use a very simple syntax, which is less expressive than a RegularExpression. Most characters in a glob are treated literally, but a * matches 0 or more characters, a ? matches precisely one character, and [...] matches any single character in a specified set (see Ranges below). All globs are implicitly anchored at both start and end.

-- glob - Greg's Wiki


Official documentation: Filename Expansion (Bash Reference Manual)

  • Related