4 ms·
That's right for $@, but AFAIK only $@ – for instance you can't do: for filename in "*.txt" ; do...
by ianbicking 11y ago
That's right for $@, but AFAIK only $@ – for instance you can't do:
for filename in "*.txt" ; do...
- adrusi 11y agoWildcards are not variables. Wildcards don't get expanded in quotes. Variables get expanded in double quotes but not single quotes. $@ obeys all the same expansion rules as all other variables. Command substitution with both $() and `` follow the same rules as variables.
- kazinator 11y ago> $@ obeys all the same rules as all other variables. That's hardy the case. Most other variables do not represent the positional parameters, and don't have the logic of "$@" which effectively produces "$1" "$2" "$3" ...
- adrusi 11y agoI was refering to the expansion behavior, which was the point in contention. I've clarified the original comment.
- hibbelig 11y agoNo, $@ is special where $* is regular. Consider the following script foo.sh and call it like so: ./foo.sh one "a b" two The $* part will print one line, the $@ part will print three lines. #!/bin/bash for x in "$*"; do echo $x done for x in "$@"; do echo $x done I use "$@" often, but to this day I don't fully understand how $@ works...
- kazinator 11y agoUnquoted $@ works exactly like unquoted $* . Quoted "$* " separates the parameters using the first character stored in IFS, or with nothing if IFS is unset/null. Usually, the first character of IFS is space, giving the impression that "$* " means "separate with spaces"; i.e. that it's just an ordinary quote job around $* (i.e. that it is "regular", in your words). Quoted "$@" does ... what you clearly understand.
- kazinator 11y agofor filename in *.txt ; do ... has no issue with spaces in filenames. If *.txt matches "foo bar.txt", then that's what the filename variable is set to. In the body of the loop you have to make sure you have "$filename". You don't need play games with IFS to correctly process filesystem entry names expanded from a pattern.