Reading User Input

Amit Jotwani
Amit JotwaniDeveloper Relations, Warp
Published: August 13, 2024

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 print-name-animal.sh 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 print-name-animal.sh
  • $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:

print-name-pet.sh

Bash
#!/bin/bash
  # 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

read-p "PROMPT" VARIABLE\_NAME

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

Bash
#!/bin/bash
  # 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)

Written by
Amit Jotwani
Amit JotwaniDeveloper Relations, Warp
Filed under

Related articles


Bash Comments

Comments will help make your scripts more readable

Curl Post Request

Use cURL to send data to a server

Bash If Statement

Learn how to use the if statement in Bash to compare multiple values and expressions.

Bash While Loop

Learn how to use and control the while loop in Bash to repeat instructions, and read from the standard input, files, arrays, and more.

Upload Files With curl

Learn how to upload a file to FTP, SFTP servers, Artifactory, and AWS S3 using the curl command.

How To Copy A Directory In Linux

Learn how to copy directories and their content in Linux using the cp command with options like -r for recursive copying, -i for interactive mode, and -a for preserving attributes.

Create Groups In Linux

Learn how to manually and automatically create and list groups in Linux.

How to Check the Size of Folders in Linux

Learn how to output the size of directories and subdirectories in a human-readable format in Linux and macOS using the du command.

Count Files in Linux

Learn how to count files and folders contained in directories and subdirectories in Linux using the ls, find, and wc commands.

List Open Ports in Linux

Learn how to output the list of open TCP and UDP ports in Linux, as well as their IP addresses and ports using the netstat command.

Format Command Output In Linux

Learn how to filter and format the content of files and the output of commands in Linux using the awk command.

Run Bash Shell In Docker

Start an interactive shell in Docker container