Difference between $ * and $ @ in Shell

Source: Internet
Author: User
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 ]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.