for file in *; do echo ${file} func ${file} case ${file} in '*') echo got asterisk ;; *) echo got something else ;; esac
echo =================== echo "${file}" func "${file}" case "${file}" in '*') echo got asterisk ;; *) echo got something else
;; esac echo =================== done
cd .. rm -rf junkdir
echo ===================
case '* X' in '*') echo got asterisk ;; *) echo got something else
;; esac
Produces:
address@hidden 24.testing# ./junk * X 2 * X got asterisk =================== * 1 * got asterisk =================== X 1 X got something else
=================== X 1 X got something else =================== =================== got something else
The for loop sees an unquoted asterisk so that expands to the files in the directory = 'x' 'X'
The for loop therefore has 2 elements in the list to work on, the first being *. Bash provides the value of file which is an asterisk to the echo statement. The echo sees an unquoted asterisk, so Bash expands the * to '*' and 'X' again, and the output confirms that.
The function call also confirms that. 2 passed parameters. The case statement obviously only sees the * as a literal. Why?
Why do some things get 2 cracks at expansion and others only 1?
The last solitary case statement shows that the one in the loop only saw the asterisk. If it saw '*' 'X', that's an error, so that can't be it.