Variables and User Input

ShellBeginner
Practice Now

Introduction

Imagine you have a robot waiter. A Static robot can only ever say "Hello, Sir" to everyone. A Dynamic robot can ask for your name and say "Hello, Sarah" or "Hello, Mike".

In the previous lab, your script always did the exact same thing. In this lab, you will make it dynamic by letting data come from outside the script itself.

This is a major shift in programming. Once a script can accept input, it stops being a fixed demo and starts becoming a reusable tool.

You will learn two ways to talk to your script:

  1. Ask & Listen: The script pauses and asks you a question (using read).
  2. Pass Notes: You slip a note to the script right when you start it (using Arguments).

By the end of this lab, your code will be able to greet anyone you want, not just "World".

Use read for Interactive Input

Our first method is the "conversational" approach. We will make the script print a question and then wait for an answer.

We will use the read command. Think of read as a command that says: "Stop everything, open your ears, and wait for the user to type something."

This is your first example of a script receiving data while it is already running. In shell scripting, a variable is a named place to store data temporarily, such as a username, filename, or number. Variables make scripts reusable because the script can work with changing input instead of hard-coded text.

First, make sure you are in your project workspace:

cd ~/project

Create a new file named hello.sh using nano:

nano hello.sh

Type the following code. Note: We define a "box" (variable) called name to store the user's answer.

#!/bin/bash

echo "What is your name?"
read name
echo "Hello, $name!"

Let's break it down:

  • echo "...": The script speaks to you.
  • read name: The script listens. Whatever you type gets put inside the name box.
  • $name: The script opens the box. The $ symbol is the key to unlock the variable and see what is inside.

Notice the difference between name and $name. name is the variable's label when you create or fill it. $name means "replace this with the value stored inside the variable." This replacement is called expansion, and it is one of the shell's most important behaviors.

Save your work (Ctrl+O, Enter) and exit (Ctrl+X).

Run the Interactive Script

Before we can chat with our script, we must authorize it to run. Remember the "Key" from the previous lab?

Grant execution permission:

chmod +x hello.sh

Now, let's have a conversation. Run the script:

./hello.sh

The script will ask "What is your name?" and then pause. It is waiting for you. The cursor might blink.

Type your name (e.g., Alice) and press Enter.

What is your name?
Alice
Hello, Alice!

This is called Interactive Input. It is great for tools used by humans, but bad for automation. Imagine trying to run this script automatically at 3 AM. It would get stuck waiting for an answer that would never come!

That tradeoff matters in real operations work. Interactive scripts are convenient when a human is present, but scheduled jobs, CI pipelines, and startup scripts usually need non-interactive input so they can run unattended.

Use Arguments for Non-Interactive Input

To make our script automation-friendly, we need a way to give it the data before it starts running, so it never has to pause. We do this using Arguments.

Think of arguments like a "mail slot" in a door. You drop the message in as you walk by.

In Bash, these mail slots have numbered names:

  • $1 is the first word you type after the script name.
  • $2 is the second word, and so on.

These numbered values are called positional parameters. They are created automatically by the shell when the script starts. You do not define $1 yourself; Bash fills it in from the command line that launched the script.

Let's upgrade hello.sh to look in the first mail slot $1 instead of asking a question.

Open the file:

nano hello.sh

Change the code to look like this:

#!/bin/bash

name=$1
echo "Hello, $name!"

Here, name=$1 takes whatever it finds in slot $1 and puts it into the name box. No pausing, no questions.

Save and exit.

Run the Script with Arguments

Now you run the script differently. You simply "throw" the name at the script when you launch it.

Run this command:

./hello.sh LabEx

Here, LabEx is the argument. The script grabs it as $1, and immediately prints:

Hello, LabEx!

Try it with another name:

./hello.sh World

It works instantly. No pauses.

The Empty Slot Problem

What happens if you don't give it an argument?

./hello.sh

You get:

Hello, !

The $1 slot was empty, so the name box was empty. In later labs, you will learn how to detect this and show an error message, but for now, you have successfully built a streamline, automation-ready script!

This difference between interactive input and command-line arguments is fundamental. read asks during execution, while $1 is available from the very beginning. Many Unix tools are designed around arguments because they are easier to automate, document, repeat, and combine with other commands.

Summary

Congratulations! You have learned the Art of Listening.

In this lab, you explored:

  1. Interactive Mode: Using read to have a conversation with the user (good for humans).
  2. Argument Mode: Using $1 to accept data at launch time (good for robots/automation).

You also learned that variables (like $name) are just labeled boxes where scripts store information. This concept is the foundation of all programming!