We can pass arguments to scripts. Create a file name hello.sh in your favorite text-editor (beginners often use nano).

#!/bin/sh
echo "Hello $1"

Make it executable and let’s run it with an argument.

./hello.sh Bob

We can use multiple arguments. Change the echo line to:

echo "Hello $1 and $2"

Then do:

./hello.sh Alice Bob

As you probably guessed, we can have as many arguments as we want.

There are a couple of special variables as well that are related to arguments.

  • $0 is the name of the script itself.
  • $# is the number of arguments.
  • $@ contains all the arguments.

Try to modify the echo to:

echo "Hello from $0 to you all $# ($@)"

Then test it with:

./hello.sh Alice Bob Charlie