0Pricing
DevOps Bootcamp · Lesson

Variables and User Input

Learn to define and use variables in your scripts, and how to prompt users for input to make scripts interactive.

Variables and User Input is a free DevOps Bootcamp lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the DevOps Bootcamp learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What are Variables?

In Bash scripting, variables are like named containers that hold data. Think of them as placeholders for information you want to use or change in your scripts.

They help make your scripts flexible, reusable, and easier to manage by storing values such as text, numbers, or even command outputs.

Declaring & Assigning Variables

To create a variable in Bash, you simply assign a value to a name. There are no special keywords like var or let needed.

  • Use VARIABLE_NAME=value syntax.
  • Important: Do not put spaces around the = sign.
  • Values can be text (strings) or numbers.

Let's try assigning some values:

#!/bin/bash

# Assigning a string
MY_NAME="CoddyKit"

# Assigning a number
LESSON_NUMBER=2

echo "Name: $MY_NAME"
echo "Lesson: $LESSON_NUMBER"

Accessing Variable Values

Once a variable is declared, you can access its value by putting a $ sign in front of its name. This tells Bash to substitute the variable name with its stored value.

For example, if you have MY_VAR="Hello", then echo $MY_VAR will print "Hello".

#!/bin/bash

CITY="New York"
TEMPERATURE=75

echo "The city is $CITY."
echo "The temperature is $TEMPERATURE degrees."

# You can also enclose in curly braces for clarity (optional)
echo "It's ${TEMPERATURE}F in ${CITY}."

Variable Naming Rules

Good variable names make your scripts easier to understand. Here are some rules and best practices:

  • Names can contain letters (a-z, A-Z), numbers (0-9), and underscores (_).
  • They must start with a letter or an underscore.
  • Variable names are case-sensitive (myVar is different from myvar).
  • It's common practice to use ALL_CAPS for environment variables and script constants, and mixedCase or snake_case for local script variables.

Read-only Variables

Sometimes you want a variable's value to remain constant throughout your script. The readonly command can be used to make a variable unchangeable.

Once a variable is set to readonly, you cannot reassign its value. Attempting to do so will result in an error.

#!/bin/bash

APP_VERSION="1.0.0"
readonly APP_VERSION

echo "App Version: $APP_VERSION"

# Uncommenting the next line would cause an error:
# APP_VERSION="1.0.1"
# echo "New App Version: $APP_VERSION"

Making Scripts Interactive

So far, our scripts have run their commands without needing any live input. But what if you want your script to ask the user for information?

This is where user input comes in handy! Interactive scripts can adapt their behavior based on what the user types, making them much more versatile and user-friendly.

The 'read' Command

The primary command for getting input from the user is read. When read is executed, the script pauses and waits for the user to type something and press Enter.

The text entered by the user is then stored in a variable you specify.

#!/bin/bash

echo "Please enter your name:"
read USER_NAME

echo "Hello, $USER_NAME! Welcome to CoddyKit."

Adding Prompts with 'read -p'

The previous example works, but it requires two lines: one for the echo prompt and one for read. You can combine these using the -p (prompt) option with read.

This makes your script's interaction much cleaner and more direct.

#!/bin/bash

read -p "What is your favorite animal? " ANIMAL

echo "Oh, so you like $ANIMAL! That's cool."

Hiding Input with 'read -s'

For sensitive information like passwords, you don't want the user's input to be displayed on the screen. The -s (silent) option with read suppresses the output as the user types.

After using read -s, it's a good practice to add an echo command to print a newline, otherwise the next prompt might appear on the same line as the hidden input.

#!/bin/bash

read -sp "Enter your secret passphrase: " PASSPHRASE
echo # This adds a newline after the hidden input

echo "Passphrase received (not displayed for security)."

Quick Check: Variables & Input

Consider the following Bash script:

#!/bin/bash

FAVORITE_FOOD="Pizza"
read -p "What's your favorite drink? " DRINK

echo "I like $FAVORITE_FOOD and $DRINK!"

Recap: Variables & User Input

Great job! You've learned the fundamentals of making your Bash scripts dynamic and interactive.

  • Variables store values using NAME=value.
  • Access variable values with $NAME.
  • readonly makes variables constant.
  • The read command gets user input.
  • Use read -p "Prompt" for inline prompts.
  • Use read -s for silent, sensitive input.

These tools are crucial for building versatile and user-friendly scripts!

Frequently asked questions

Is the “Variables and User Input” lesson free?

Yes — the full text of “Variables and User Input” is free to read here on the web, and the DevOps Bootcamp course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the DevOps Bootcamp course, upgrade to CoddyKit PRO.

What will I learn in “Variables and User Input”?

Learn to define and use variables in your scripts, and how to prompt users for input to make scripts interactive. You practise DevOps Bootcamp with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start DevOps Bootcamp?

No prior experience is required. DevOps Bootcamp on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Variables and User Input” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this DevOps Bootcamp lesson?

Yes. Every DevOps Bootcamp lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. First Bash Script & Shebang
  2. Variables and User Input
  3. Conditional Statements (if, else, elif)
  4. Working with Arrays in Bash
← Back to DevOps Bootcamp