-2

x=('hello world' "HELLO")

Both ${#x[*]} and ${#x[@]} print the same output.

I understand the difference between $@ and $* but I am interested to see the difference without command line arguments.

Suraj
  • 27
  • 7
  • `${#...}` doesn't subject the contents of the array to further shell expansion, so those two expressions *are* identical. – chepner Oct 29 '16 at 13:40

1 Answers1

0

Always use @ expansion unless you have reason to use *. @ was added to work around a problem.

The two don't ALWAYS expand the same. The troubles involving* start with spaces and other shell metacharacters (quotes in particular, but $ and more as well).

The * leaves the metacharacters open for the shell to process them again, which is usually bad if you went out of your way to get them into the array. The @ protects them by expanding each array element as if it was a separately quoted value, leaving all metacharacters intact.

Gilbert
  • 3,740
  • 17
  • 19