Bash 인자 전달 방법

Bash (Bourne Again Shell) is a popular command-line shell and scripting language used in Linux and UNIX systems. It provides a powerful interface for interacting with the operating system and executing commands. One fundamental aspect of Bash scripting is the ability to pass arguments to scripts or functions.

In this blog post, we will explore different methods for passing arguments in Bash and how they can be used in Linux.

Positional Arguments

The most common way to pass arguments to a Bash script or function is through positional arguments. The arguments are passed to the script in a sequential order and can be accessed using special variables called positional parameters.

#!/bin/bash

# script.sh

echo "The script name is: $0"
echo "The first argument is: $1"
echo "The second argument is: $2"
echo "All arguments are: $@"

In the example above, the script is called script.sh, and the positional arguments are accessed using $1, $2, and $@. The $0 variable represents the name of the script itself.

To execute the script with arguments, use the command:

$ ./script.sh argument1 argument2

The output will be:

The script name is: ./script.sh
The first argument is: argument1
The second argument is: argument2
All arguments are: argument1 argument2

Named Arguments

While Bash doesn’t natively support named arguments, you can mimic their behavior by using key-value pairs or options passed as positional arguments. You can then parse these arguments manually within your script.

#!/bin/bash

for arg in "$@"
do
  case $arg in
    --name=*)
    name="${arg#*=}"
    ;;
    --age=*)
    age="${arg#*=}"
    ;;
    *)
    # Unknown argument
    ;;
  esac
done

echo "Name: $name"
echo "Age: $age"

To execute the script with named arguments, use the command:

$ ./script.sh --name=John --age=25

The output will be:

Name: John
Age: 25

Command-Line Options

Another common way to pass arguments to a Bash script is through command-line options. These options are preceded by a dash (-) and a single character or a double dash (–) followed by a meaningful word.

To parse command-line options, you can use the getopts function. It allows you to define the options your script accepts and handles them accordingly.

Here’s an example:

#!/bin/bash

while getopts ":n:a:" opt; do
  case $opt in
    n)
      name=$OPTARG
      ;;
    a)
      age=$OPTARG
      ;;
    \?)
      echo "Invalid option: -$OPTARG" >&2
      ;;
  esac
done

echo "Name: $name"
echo "Age: $age"

To execute the script using command-line options, use the command:

$ ./script.sh -n John -a 25

The output will be the same as the named arguments example:

Name: John
Age: 25

Conclusion

Passing arguments to Bash scripts or functions is crucial for building more flexible and dynamic scripts. In this blog post, we explored different methods for passing arguments in Bash, including positional arguments, named arguments, and command-line options. Understanding these techniques will help you create more powerful and customizable scripts in Linux.