Difference between $ * and $ @ in Shell
A little bit about $ * and $ @ is also learned by cainiao.
$ *
All location parameters are used as a word.
Note: "$ *" must be referenced.
$ @
It is synonymous with $ *, but each parameter is an independent "" reference string, which means that the parameter is completely passed,
It is not interpreted and extended. This also means that each parameter in each parameter list is treated as an independent
Word.
Note: "$ @" must be referenced.
$ @ $ * Difference only when enclosed in double quotes
Double quotation marks:
$ * Consider all parameters as a field
$ @ Use ifs (space by default) to divide fields. If the space is in "", it is not divided. Use the LS script to run./Test 1 "2 3" 4 to find the difference
The case is that $ @ is the same as $ *. If you see ifs, the field is partitioned. Use the LS script to run./Test 1 "2 3" 4 to find the difference
A small example for reference only
[Copy to clipboard]
[-]Code :#! /Bin/bash
Echo
Index = 1
Echo "listing ARGs with \" \ $ *\":"
For ARG in "$ *"
Do
Echo "Arg # $ Index = $ Arg"
Let "index + = 1"
Done
Echo "All parameters are considered a word"
Echo
Index = 1
Echo "listing ARGs with \" \$ @\":"
For ARG in "$ @"
Do
Echo "Arg # $ Index = $ Arg"
Let "index + = 1"
Done
Echo "All parameters are considered independent words"
Echo
Index = 1
Echo "listing ARGs with \ $ * (not referenced ):"
For ARG in $ *
Do
Echo "Arg # $ Index = $ Arg"
Let "index + = 1"
Done
Echo "All parameters are considered independent words"
Exit 0
After running, the output is
[Copy to clipboard]
[-]Code: [root @ localhost ABS] #./Test 1 2 3 4
Listing ARGs with "$ *":
Arg #1 = 1 2 3 4
All parameters are considered as a word.
Listing ARGs with "$ @":
Arg #1 = 1
Arg #2 = 2
Arg #3 = 3
Arg #4 = 4
All parameters are considered independent words.
listing ARGs with $ * (not referenced ):
Arg #1 = 1
Arg #2 = 2
Arg #3 = 3
Arg #4 = 4
All parameters are considered is an independent word
[ edited by at the end of this post ]