Bash: Why doesn't * return empty string in case of no match

Fredrik Johansson

I don't understand why * returns itself when there's no match. This answer states that this is the case, but I don't understand the rationale for this behaviour. Why doesn't *, or any generic pattern, just return an empty string in case of no match?

$ls      # Look, no files
$touch a # Add a file
$echo *  # Works as expected
a
$rm a    # Remove the file
$echo *  # Will output a '*'. Why not ''?
*
$*       # Ok, * will fail, but how come the previous output?
-bash: *: command not found
$
trishnendu

The * is actually being expanded by bash (whatever your shell is) and then it is passed on to the echo. The echo command does nothing but prints it to standard output. Read this document - File name expansion. It says-

Bash scans each word for the characters "*", "?", and "[". If one of these characters appears, then the word is regarded as a PATTERN, and replaced with an alphabetically sorted list of file names matching the pattern. If no matching file names are found, and the shell option nullglob is disabled, the word is left unchanged. If the nullglob option is set, and no matches are found, the word is removed.

So actually when bash cannot find any matching it left '*' unchanged and echo prints that.

Reason The reason bash doesn't return an empty statement by default cause there can be cases where the user doesn't want to use the '*' as wildcard and so he doesn't need the expansion of '*'. And hence in this case expanding the '*' to empty string would have erroneous. However this default behaviour can be overridden by nullglob option.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why doesn't the Select statement assigns an empty string or null value if it doesn't return a result?

From Dev

Split doesn't return empty string

From Dev

Why does String.match( / \d*/ ) return an empty string?

From Dev

Why doesn't empty string == null in JavaScript

From Dev

Return empty string for no match on DateTime

From Dev

Why doesn't re.match("c", "cat") return true?

From Dev

Why doesn't std::regex_match return true with $ pattern?

From Dev

Elixir - Split a string so it doesn't return empty strings in the list?

From Dev

Why doesn't this string.Format() return string, but dynamic?

From Dev

Why doesn't String.tap return the modified string?

From Dev

Why doesn't this simple bash regex return true?

From Java

Why doesn't String switch statement support a null case?

From Dev

Why string concat macro doesn't work for this "+" case?

From Dev

Why doesn't the regex ^([0|1]1)+$ match the string "111"?

From Dev

Why doesn't this JavaScript object key match a string with the same name?

From Dev

Why does this return the empty string?

From Dev

Why Promise return an empty string?

From Dev

Why split function doesn't return a null at the first of this string?

From Dev

C# Regex to match a Case Insensitive string that doesn't contain a certain string

From Dev

Why doesn't this regex match?

From Dev

Regular Expression Match Doesn't Start With, Contain, Or End With Space And Not Empty String

From Dev

Why it doesn't return?? PHP

From Dev

Why doesn't it return a value?

From Dev

Why doesn't this return the price?

From Dev

Why a return statement of my user defined function doesn't work at some case?

From Dev

why doesn't python return result using regex when there's more than one match?

From Dev

Grep doesn't match carriage return characters

From Dev

RankNTypes doesn't match return type

From Dev

Grep doesn't match carriage return characters

Related Related

  1. 1

    Why doesn't the Select statement assigns an empty string or null value if it doesn't return a result?

  2. 2

    Split doesn't return empty string

  3. 3

    Why does String.match( / \d*/ ) return an empty string?

  4. 4

    Why doesn't empty string == null in JavaScript

  5. 5

    Return empty string for no match on DateTime

  6. 6

    Why doesn't re.match("c", "cat") return true?

  7. 7

    Why doesn't std::regex_match return true with $ pattern?

  8. 8

    Elixir - Split a string so it doesn't return empty strings in the list?

  9. 9

    Why doesn't this string.Format() return string, but dynamic?

  10. 10

    Why doesn't String.tap return the modified string?

  11. 11

    Why doesn't this simple bash regex return true?

  12. 12

    Why doesn't String switch statement support a null case?

  13. 13

    Why string concat macro doesn't work for this "+" case?

  14. 14

    Why doesn't the regex ^([0|1]1)+$ match the string "111"?

  15. 15

    Why doesn't this JavaScript object key match a string with the same name?

  16. 16

    Why does this return the empty string?

  17. 17

    Why Promise return an empty string?

  18. 18

    Why split function doesn't return a null at the first of this string?

  19. 19

    C# Regex to match a Case Insensitive string that doesn't contain a certain string

  20. 20

    Why doesn't this regex match?

  21. 21

    Regular Expression Match Doesn't Start With, Contain, Or End With Space And Not Empty String

  22. 22

    Why it doesn't return?? PHP

  23. 23

    Why doesn't it return a value?

  24. 24

    Why doesn't this return the price?

  25. 25

    Why a return statement of my user defined function doesn't work at some case?

  26. 26

    why doesn't python return result using regex when there's more than one match?

  27. 27

    Grep doesn't match carriage return characters

  28. 28

    RankNTypes doesn't match return type

  29. 29

    Grep doesn't match carriage return characters

HotTag

Archive