Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the difference between $* and $@ in Shell

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "what is the difference between $* and $@ in Shell". In daily operation, I believe many people have doubts about the difference between $* and $@ in Shell. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the question of "what is the difference between $* and $@ in Shell"! Next, please follow the editor to study!

When $* and $@ are not surrounded by double quotation marks, there is no difference between them, each parameter received is treated as a piece of data, separated by spaces.

But when they are enclosed in double quotes, there is a difference:

"$*" treats all parameters as a data as a whole, rather than each parameter as a data.

"$@" still treats each parameter as a piece of data, independent of each other.

For example, if five parameters are passed, for "$*", the five parameters will be merged together to form a piece of data, which can not be separated; for "$@", the five parameters are independent of each other. They are five pieces of data.

If you use echo to output "$" and "$@" directly, you can't see the difference, but if you use the for loop to output data one by one, you can immediately see the difference.

For the use of for loops, please click: Shell for loops and for int loops

Write the following code and save it as test.sh:

#! / bin/bash

Echo "print each param from\"\ $\ ""

For var in "$"

Do

Echo "$var"

Done

Echo "print each param from\"\ $@\ ""

For var in "$@"

Do

Echo "$var"

Done

Run test.sh with parameters:

[mozhiyan@localhost demo] $. . / test.sh a b c d

Print each param from "$"

A b c d

Print each param from "$@"

A

B

C

D

From the running results, you can see that for "$*", it only loops once because it has only 1 point of data; for "$@", it loops five times because it has five pieces of data.

At this point, the study on "what is the difference between $* and $@ in Shell" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report