Bash script arguments example

In this post, I will shortly share Bash script arguments via an easy example so it can help you understand how it works and write a bash script with parameters input from command line in terminal.

Let’s imagine the following command line:

[[email protected] me]$ your_bash_program word1 word2 word3

If your_bash_program were a bash shell script, we could read each item on the command line because the positional parameters contain the following:

  • $0 would contain “your_bash_program
  • $1 would contain “word1”
  • $2 would contain “word2”
  • $3 would contain “word3”

Here is a script you can use to try this out:

#!/bin/bash

echo "List Parameters"
echo '$0 = ' $0
echo '$1 = ' $1
echo '$2 = ' $2
echo '$3 = ' $3

Also published on Medium.

Leave a Reply

Your email address will not be published. Required fields are marked *