Friday, August 1, 2025

Passing Values into Shell Scripts: A Beginner’s Guide with Examples

Shell scripting is a powerful tool for automating repetitive tasks in Linux. One of the key aspects of writing flexible and reusable shell scripts is the ability to pass input values into the script in various ways. Whether you're accepting command-line arguments, reading user input, or sourcing data from a file, shell scripting offers multiple mechanisms to handle data efficiently. These techniques are essential for writing scripts that can be reused in different scenarios—from system automation and cron jobs to configuration management and interactive tools.

In this post, we’ll explore five different ways to pass values into a shell script, using a common use case:

Use Case: Accept a value x and print it.

1. Hardcoded value 
In this method, the value is fixed directly in the script. This is simple but not suitable for reusable scripts.

Example:
#!/bin/bash
x=10
echo "Value of x is $x"

Best for: 
 Quick testing and debugging
 one-time-use scripts


2. Get the values from the user 
This method prompts the user for input at runtime using the read command.

Example: 

#!/bin/bash
# Method 2: Get input from user
read -p "Enter the value of x: " x
echo "Value of x is $x"

 Best for: Interactive scripts requiring user input.

3. Command-Line Arguments (Positional Parameters)
This method reads input from command-line arguments. The first argument is stored in $1, the second in $2, and so on.

#!/bin/bash
# Method 3: Use positional parameter
if [ -z "$1" ]; then
    echo "Usage: $0 <x>"
    exit 1
fi

x=$1
echo "Value of x is $x"

Run as: ./script.sh 10

Best for: Scheduled jobs or automation scripts.

4. read the values from the file 
This method reads the input from a file. It’s especially useful for batch processing.

#!/bin/bash
# Method 4: Read from file
file="input.txt"

if [ ! -f "$file" ]; then
    echo "Input file not found!"
    exit 1
fi

read -r x < "$file"
echo "Value of x is $x"

Best for: Reading config values or input data in bulk.



5. using flags or Named arguments 
For professional scripts or tools, you can use named arguments like -x to accept values using getopts.

#!/bin/bash
# Method 5: Use named flag with getopts

while getopts x: flag; do
  case "${flag}" in
    x) x=${OPTARG};;
    *) echo "Usage: $0 -x <value>"; exit 1;;
  esac
done

if [ -z "$x" ]; then
    echo "Usage: $0 -x <value>"
    exit 1
fi

echo "Value of x is $x"

# Run this script as:
./script.sh 10

 Best for: CLI tools with multiple named inputs.


Conclusion 

Choosing the right method to pass values into your shell script depends on your use case. For scripts requiring automation and integration, command-line arguments and flags are preferred. For interactive scripts, use read. For batch jobs, file input is efficient.


| Method                 | Best Use Case                                |
| ---------------------  | ---------------------------------------- |
| In Code                 | Quick testing                                 |
| `read`                    | Interactive input                            |
| Positional Params | Standard CLI usage                      |
| File Input              | Batch processing                           |
| Flags (`getopts`)   | Reusable tools with flexible input |

No comments:

Post a Comment

Passing Values into Shell Scripts: A Beginner’s Guide with Examples

Shell scripting is a powerful tool for automating repetitive tasks in Linux. One of the key aspects of writing flexible and reusable shell s...