国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Table of Contents
$# – Count the Number of Arguments
$* – All Arguments as One String
$@ – All Arguments as Separate Strings
Home System Tutorial LINUX What is the difference between $*, $@, and $# in shell scripts?

What is the difference between $*, $@, and $# in shell scripts?

Jul 15, 2025 am 01:19 AM
variable

In shell scripts, special variables $#, $*, and @ are used to handle command line parameters and their behavior varies. 1. $# represents the number of parameters passed to the script, which is suitable for checking whether the number of parameters is correct; 2. $* treats all parameters as a whole string, and is connected by spaces by default, which is suitable for logging or passing parameters as a whole, but the original spaces or quotes may not be retained; 3. $@ treats each parameter as an independent string, retains original separation, which is suitable for traversing parameters or passing parameters with spaces such as file names. These variables should always be wrapped in quotes when using them to avoid problems caused by special characters.

What is the difference between $*, $@, and $# in shell scripts?

When working with shell scripts, especially in Bash, you'll often see special variables like $* , $@ , and $# . These variables are used to access command-line arguments passed to a script, but they behave differently depending on how you use them. Here's a breakdown of what each one does and when to use it.


$# – Count the Number of Arguments

This is the simplest one: $# gives you the number of arguments passed to the script.

For example:

 #!/bin/bash
echo "You provided $# arguments"

If you run this script like so:

 ./script.sh apple banana cherry

It will output:

 You provided 3 arguments

Use $# when you need to check if the user provided the correct number of inputs or handle cases where the argument count affects logic flow.


$* – All Arguments as One String

$* treats all the positional arguments as a single string, joined together with spaces (or the first character of IFS , which defaults to space).

Example:

 #!/bin/bash
echo "All arguments: $*"

Running:

 ./script.sh one two three

Will print:

 All arguments: one two three

This can be useful if you just want to log or pass all arguments as a single unit, but be careful — it may not preserve spacing or quoting properly in some contexts.


$@ – All Arguments as Separate Strings

$@ behaves similarly to $* , but with an important difference: it preserves the separation between arguments. Each argument is treated as its own string.

Example:

 #!/bin/bash
for arg in "$@"
do
  echo "Argument: $arg"
done

Running:

 ./script.sh "hello there" goodbye

Will result in:

 Argument: hello there
Argument: goodbye

Whereas if you replace "$@" with "$*" in that loop, it would treat "hello there goodbye" as one argument.

So if you're looping through arguments or passing them to another command while preserving their original structure, always use "$@" .


Here's a quick summary:

  • Use $# when you need to know how many arguments were given.
  • Use $* when you want to treat all arguments as a single string (less common).
  • Use $@ when you want to handle each argument individually , especially when dealing with filenames or phrases that contain spaces.

And remember: always quote these variables unless you have a specific reason not to — otherwise, spaces and special characters can cause unexpected behavior.

Basically that's it.

The above is the detailed content of What is the difference between $*, $@, and $# in shell scripts?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

A guide to using Windows 11 and 10 environment variables for profiling A guide to using Windows 11 and 10 environment variables for profiling Nov 01, 2023 pm 08:13 PM

Environment variables are the path to the location (or environment) where applications and programs run. They can be created, edited, managed or deleted by the user and come in handy when managing the behavior of certain processes. Here's how to create a configuration file to manage multiple variables simultaneously without having to edit them individually on Windows. How to use profiles in environment variables Windows 11 and 10 On Windows, there are two sets of environment variables – user variables (apply to the current user) and system variables (apply globally). However, using a tool like PowerToys, you can create a separate configuration file to add new and existing variables and manage them all at once. Here’s how: Step 1: Install PowerToysPowerTo

Strict mode for variables in PHP7: how to reduce potential bugs? Strict mode for variables in PHP7: how to reduce potential bugs? Oct 19, 2023 am 10:01 AM

Strict mode was introduced in PHP7, which can help developers reduce potential errors. This article will explain what strict mode is and how to use strict mode in PHP7 to reduce errors. At the same time, the application of strict mode will be demonstrated through code examples. 1. What is strict mode? Strict mode is a feature in PHP7 that can help developers write more standardized code and reduce some common errors. In strict mode, there will be strict restrictions and detection on variable declaration, type checking, function calling, etc. Pass

What are instance variables in Java What are instance variables in Java Feb 19, 2024 pm 07:55 PM

Instance variables in Java refer to variables defined in the class, not in the method or constructor. Instance variables are also called member variables. Each instance of a class has its own copy of the instance variable. Instance variables are initialized during object creation, and their state is saved and maintained throughout the object's lifetime. Instance variable definitions are usually placed at the top of the class and can be declared with any access modifier, which can be public, private, protected, or the default access modifier. It depends on what we want this to be

Internal error: Unable to create temporary directory [Resolved] Internal error: Unable to create temporary directory [Resolved] Apr 17, 2023 pm 03:04 PM

Windows system allows users to install various types of applications on your system using executable/setup files. Recently, many Windows users have started complaining that they are receiving an error named INTERNALERROR:cannotCreateTemporaryDirectory on their systems while trying to install any application using an executable file. The problem is not limited to this but also prevents the users from launching any existing applications, which are also installed on the Windows system. Some possible reasons are listed below. Run the executable to install without granting administrator privileges. An invalid or different path was provided for the TMP variable. damaged system

Mind map of Python syntax: in-depth understanding of code structure Mind map of Python syntax: in-depth understanding of code structure Feb 21, 2024 am 09:00 AM

Python is widely used in a wide range of fields with its simple and easy-to-read syntax. It is crucial to master the basic structure of Python syntax, both to improve programming efficiency and to gain a deep understanding of how the code works. To this end, this article provides a comprehensive mind map detailing various aspects of Python syntax. Variables and Data Types Variables are containers used to store data in Python. The mind map shows common Python data types, including integers, floating point numbers, strings, Boolean values, and lists. Each data type has its own characteristics and operation methods. Operators Operators are used to perform various operations on data types. The mind map covers the different operator types in Python, such as arithmetic operators, ratio

How to get variables from PHP method using Ajax? How to get variables from PHP method using Ajax? Mar 09, 2024 pm 05:36 PM

Using Ajax to obtain variables from PHP methods is a common scenario in web development. Through Ajax, the page can be dynamically obtained without refreshing the data. In this article, we will introduce how to use Ajax to get variables from PHP methods, and provide specific code examples. First, we need to write a PHP file to handle the Ajax request and return the required variables. Here is sample code for a simple PHP file getData.php:

jQuery usage practice: several ways to determine whether a variable is empty jQuery usage practice: several ways to determine whether a variable is empty Feb 27, 2024 pm 04:12 PM

jQuery is a JavaScript library widely used in web development. It provides many simple and convenient methods to operate web page elements and handle events. In actual development, we often encounter situations where we need to determine whether a variable is empty. This article will introduce several common methods of using jQuery to determine whether a variable is empty, and attach specific code examples. Method 1: Use the if statement to determine varstr="";if(str){co

Deep understanding of const in C language Deep understanding of const in C language Feb 18, 2024 pm 12:56 PM

Detailed explanation and code examples of const in C In C language, the const keyword is used to define constants, which means that the value of the variable cannot be modified during program execution. The const keyword can be used to modify variables, function parameters, and function return values. This article will provide a detailed analysis of the use of the const keyword in C language and provide specific code examples. const modified variable When const is used to modify a variable, it means that the variable is a read-only variable and cannot be modified once it is assigned a value. For example: constint

See all articles