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

Table of Contents
Accessing Arguments with Positional Parameters
Handling Multiple or Optional Arguments
Using getopts for Command-Line Options
Home System Tutorial LINUX How to pass arguments to a bash script?

How to pass arguments to a bash script?

Jun 18, 2025 am 12:23 AM
Parameter passing bash script

Passing parameters in a Bash script can be achieved by directly entering parameters after the script name. These parameters are accessed in the script through position variables such as $1 and $2. For example, when running ./greet.sh Alice, $1 means Alice. If you need to process multiple parameters, you can use a loop to traverse all parameters with "$@". In addition, the getopts function can parse parameters with options (such as -n or -a) to improve script flexibility and ease of use.

How to pass arguments to a bash script?

You can pass arguments to a bash script by simply typing them after the script name when you run it. The syntax looks like this:

 ./script.sh arg1 arg2 arg3

The arguments ( arg1 , arg2 , etc.) are then accessible inside the script using special variables like $1 , $2 , $3 , and so on.


Accessing Arguments with Positional Parameters

Inside your bash script, each argument is available via positional parameters:

  • $0 – the name of the script itself
  • $1 – the first argument
  • $2 – the second argument
  • ...and so on

For example, if your script is called greet.sh and contains this:

 echo "Hello $1"

Then running:

 ./greet.sh Alice

Will output:

 Hello Alice

This is the most basic and commonly used way to handle script arguments.

If you have more than 9 arguments, use ${10} , ${11} , etc., since just $10 would be interpreted as $1 followed by the number 0.


Handling Multiple or Optional Arguments

Sometimes you want to allow multiple or optional inputs. Bash makes this easy with built-in variables like:

  • $@ – all arguments as separate strings
  • $* – all arguments as one single string
  • $# – the number of arguments passed

A common use case is looping through all arguments:

 for name in "$@"
do
    echo "Hello $name"
done

Running:

 ./greet.sh Alice Bob Charlie

Would print:

 Hello Alice
Hello Bob
Hello Charlie

This approach gives you flexibility when handling variable numbers of inputs.

Also, you can check how many arguments were given using $# . For example:

 if [ $# -lt 2 ]; then
    echo "Need at least two arguments!"
    exit 1
fi

This ensures your script doesn't run with incomplete input.


Using getopts for Command-Line Options

If your script supports flags or options (like -n or -v ), getopts is the built-in tool for parsing them.

Here's a simple example that accepts -n for name and -a for age:

 while getopts n:a: option; do
    case $option in
        n) NAME=$OPTARG;;
        a) AGE=$OPTARG;;
    esac
done

echo "Name: $NAME, Age: $AGE"

Run it like this:

 ./script.sh -n Alice -a 30

It keeps things clean when dealing with scripts that accept configuration flags or settings from the command line.

Keep in mind that options with arguments need a colon after their letter in the getopts string (eg, n: means -n expects a value).


That's basically it — you can start with positional parameters for simple needs, and move to getopts when you want to support flags and options. It's not complicated, but getting the syntax right helps avoid bugs later.

The above is the detailed content of How to pass arguments to a bash script?. 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)

Best practices for optimizing Golang function parameter passing performance Best practices for optimizing Golang function parameter passing performance Apr 13, 2024 am 11:15 AM

In order to optimize Go function parameter passing performance, best practices include: using value types to avoid copying small value types; using pointers to pass large value types (structures); using value types to pass slices; and using interfaces to pass polymorphic types. In practice, when passing large JSON strings, passing the data parameter pointer can significantly improve deserialization performance.

What is the parameter passing method for PHP functions? What is the parameter passing method for PHP functions? Apr 10, 2024 am 11:06 AM

There are two ways to pass parameters in PHP: call by value (the parameter is passed as a copy of the value, modification within the function does not affect the original variable) and passing by reference (the address of the parameter is passed, modification within the function will affect the original variable), when the original variable needs to be modified Use reference passing when calculating the shopping cart total price, which requires reference passing to calculate correctly.

How to pass parameters to PHP function? How to pass parameters to PHP function? Apr 10, 2024 pm 05:21 PM

PHP functions can pass values ??through parameters, which are divided into pass by value and pass by reference: pass by value: modification of parameters within the function will not affect the original value; pass by reference: modification of parameters within the function will affect the original value. In addition, arrays can also be passed as parameters for operations such as calculating the sum of data.

Detailed explanation of C++ function parameters: Comparison of parameter passing methods of different pointer types Detailed explanation of C++ function parameters: Comparison of parameter passing methods of different pointer types Apr 27, 2024 am 09:27 AM

There are three ways to pass pointer parameters in C++: passing by value, passing by reference, and passing by address. Passing by value copies the pointer without affecting the original pointer; passing by reference allows the function to modify the original pointer; passing by address allows the function to modify the value pointed to by the pointer. Choose the appropriate parameter transmission method according to your needs.

Golang formal parameter requirements guide: parameter passing methods, value passing and address passing Golang formal parameter requirements guide: parameter passing methods, value passing and address passing Mar 02, 2024 pm 05:18 PM

Guide to Golang formal parameter requirements: parameter passing methods, value passing and address passing. In the process of learning the Golang programming language, it is very important to understand the parameter passing methods and the concepts of value passing and address passing. This article will delve into the formal parameter requirements in Golang, including the difference between parameter passing methods, value passing and address passing, and provide specific code examples to help readers better understand. 1. Parameter passing methods In Golang, there are two methods of parameter passing for functions: passing by value and passing by address. Pass by value (pass a copy): When the function is called, the actual

Research on parameter passing methods in Go language Research on parameter passing methods in Go language Apr 03, 2024 pm 02:48 PM

In the Go language, there are two main ways to pass function parameters: value passing: passing a copy of the variable will not affect the original variable in the calling code. Pointer passing: Passing the address of a variable allows the function to directly modify the original variable in the calling code.

Detailed explanation of C++ function parameters: Performance optimization of parameter passing in parallel programming Detailed explanation of C++ function parameters: Performance optimization of parameter passing in parallel programming Apr 27, 2024 pm 02:09 PM

In a multi-threaded environment, function parameter passing methods are different, and the performance difference is significant: passing by value: copying parameter values, safe, but large objects are expensive. Pass by reference: Passing by reference is efficient, but function modifications will affect the caller. Pass by constant reference: Pass by constant reference, safe, but restricts the function's operation on parameters. Pass by pointer: Passing pointers is flexible, but pointer management is complex, and dangling pointers or memory leaks may occur. In parallel summation, passing by reference is more efficient than passing by value, and passing by pointer is the most flexible, but management is complicated.

The relationship between C++ function parameter passing methods and class inheritance The relationship between C++ function parameter passing methods and class inheritance Apr 12, 2024 am 11:06 AM

Question: What is the relationship between C++ function parameter passing methods and class inheritance? Answer: When a subclass inherits a parent class function, the parameter passing method can be the same or different. If the subclass does not override the parent class function, it inherits the parent class's parameter passing method. If a subclass overrides a parent class function, it can choose to use a different parameter passing method. When a subclass needs to modify parameters in a parent class function, it needs to declare the parent class function as pass-by-reference.

See all articles