Reading User Input

Last Updated 2022-09-09

There are a few different ways your bash program can accept user input

  1. Using Command Line Arguments
  2. Prompt for input during script execution

Accepting User Input with Command Line Arguments

When you run a command on the command line, you can supply arguments to them that act as inputs for that command.

For instance, when you run ls -l /Documents, the `-l` and `/Documents` are both command line arguments to the command `ls`.

You can do the same thing while writing your own bash scripts. For example, by passing "Amit" and "dog" as positional parameter arguments -

`bash amit dog`

When you run this command, bash will automatically provide the following positional parameter variables to our script, each containing the contents of the command line.

  • $0 - The variable $0 is set to the first word of the command line statement - the name of your bash script. In this case, it will be
  • $1 - First parameter after the command name, in this case it will be set to amit
  • $2 - Second parameter after the command name, in this case it will be set to dog

If there was a third parameter, its value would be placed in the variable $3, and so on.

These shell variables are automatically set by the shell when we run our script so all we need to do is refer to them in our script, like:

  # a simple bash script that accepts user's name and favorite pet animal as command line arguments and prints them

  echo "Your name is $1"
  echo "Your favorite pet animal is $2"

But, what if you had more than 9 parameters?

Parameters greater than 9 can be accessed using curly braces around the number; for instance, ${10} would be the tenth parameter, and ${123} would be the 123rd.

Did you know?

You can also check the number of positional parameters (excluding $0) by using the variable $#.

You can also access all the items on the command line starting at the first parameter ($1), with the special variable $@.

Best Practices

Positional parameters are delimited by a space. The shell interprets the things after the spaces as individual parameters. If the parameter itself contains a space, enclose it in quotation marks, like "Snow Leopard".

Prompt for input using read

In addition to using command line arguments as input, you can also prompt the user for input during the script execution. You do that using the read command.

The syntax for the read command is


Here’s an example for using it ask the user for their name and favorite pet:

  # simple bash script that asks the user for their name and favorite animal, and prints it
  read -p "Hello! What's your first name? " first_name
  read -p "What's your favorite pet? " favorite_pet
  echo  "Nice to meet you $first_name! I now know your favorite pet is" $favorite_pet

How it works

The read command here captures the input from the user and stores it in our variables - $first_name and $favorite_animal

Read Command Options

The read command provides a few options/flags you can use:

  • -p Prompt: You can use this to provide a helpful message for the user before the input prompt. For example, read -p “What’s your height (inches)?” user_height
  • -s Silent Mode: You can use this to accept invisible input from the user. Useful for passwords or other secret information.
  • -t Timed Input: You can use this to set time in seconds that your script should wait for taking input from the user.

Choosing Command Line Arguments vs Prompting for execution

Accept user input through command line arguments (preferred, stays in history, making it easier for users to run the command again, without waiting for manual typing for input)