How to write a Bash script
Help 5 / 9
User input

Parsing command-line arguments

Scripts are most useful when you can change their behavior without modifying the script itself, which is what command-line parameters are for.

For example, let’s update our script to take 2 parameters: a greeting and a name, and have it output the greeting followed by the name.

We can do that by storing the value of each command-line parameter in its own variable. The first parameter is represented by the variable $1, and the second parameter by $2:

#!/bin/bash
greeting=$1
name=$2

echo "$greeting $name"

Try running this script:

./hello.sh 'Good morning' Robert

Loading...