0Pricing
Linux Command Line Mastery · Lesson

Variables, Input, and Output

Learn to declare variables, accept user input, and manage script output in shell scripts.

Variables, Input, and Output is a free Linux Command Line Mastery lesson on CoddyKit — lesson 1 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 Linux Command Line Mastery learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What are Script Variables?

In shell scripting, variables are like containers for storing data. They help make your scripts flexible and reusable.

Instead of hardcoding values, you can use variables to hold information such as filenames, user inputs, or temporary results.

Declaring & Assigning Variables

To create a variable, you simply assign a value to a name. No special keyword is needed!

  • Use NAME="Coddy" (no spaces around =).
  • Values with spaces usually need quotes (single or double).
  • Numbers don't strictly need quotes, but it's often good practice for consistency.

Accessing Variable Values

To use the value stored in a variable, you prepend its name with a $ sign.

For example, if you declared GREETING="Hello", you'd use $GREETING to get "Hello". You can also use ${VARIABLE}, which is clearer in complex situations.

Variable Naming Tips

Follow these simple rules for variable names:

  • Start with a letter or underscore.
  • Can contain letters, numbers, and underscores.
  • Avoid using special shell keywords.
  • Common practice: use uppercase for global/environment variables, lowercase for script-local ones.

Demo: Using Variables

Let's see a simple script that declares and uses a variable.

Notice how $MESSAGE retrieves the stored text.

#!/bin/bash

# Declare a variable
MY_NAME="Coddy"
GREETING="Hello"

# Access and print variables
echo "$GREETING, $MY_NAME!"

# Update the variable
MY_NAME="Learner"
echo "Nice to meet you, $MY_NAME!"

Getting User Input: `read`

The read command is essential for making your scripts interactive. It waits for the user to type something and press Enter.

  • read VARIABLE: Stores input into VARIABLE.
  • read -p "Prompt: " VARIABLE: Displays a prompt before waiting for input.

Demo: Taking User Input

This script uses read -p to ask for your name and then greets you personally.

Try running it and entering your name!

#!/bin/bash

# Prompt for user's name and store it
read -p "What's your name? " USER_NAME

# Greet the user
echo "Hello, $USER_NAME! Welcome to CoddyKit."

Script Output: `echo` & `printf`

You've seen echo for basic output. It simply prints its arguments followed by a newline.

For more control, printf (like in C) allows formatted output, specifying data types and alignment. It doesn't add a newline automatically, so use \n.

Demo: Input, Output & Variables

This example combines variables, user input with read, and formatted output using printf.

It calculates a simple sum based on your input.

#!/bin/bash

# Declare a base number
BASE_NUM=10

# Get a number from the user
read -p "Enter another number: " USER_NUM

# Calculate the sum
# Use $((...)) for arithmetic operations
SUM=$((BASE_NUM + USER_NUM))

# Print the result using printf for formatting
printf "The sum of %d and %d is %d.\n" "$BASE_NUM" "$USER_NUM" "$SUM"

Quick Check

Consider the following shell script snippet:

#!/bin/bash
NAME="Alice"
read -p "Enter a city: " CITY
echo "Hello, $NAME from $CITY!"

If the user types London and presses Enter, what will be the final output?

Recap: Variables, Input, Output

Great job! You've learned the fundamentals of shell scripting:

  • Variables store data using VAR="value".
  • Access variables with $VAR or ${VAR}.
  • read gets user input, often with -p for a prompt.
  • echo prints simple messages.
  • printf offers formatted output.

These are building blocks for dynamic and interactive scripts!

Frequently asked questions

Is the “Variables, Input, and Output” lesson free?

Yes — the full text of “Variables, Input, and Output” is free to read here on the web, and the Linux Command Line Mastery 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 Linux Command Line Mastery course, upgrade to CoddyKit PRO.

What will I learn in “Variables, Input, and Output”?

Learn to declare variables, accept user input, and manage script output in shell scripts. You practise Linux Command Line Mastery 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 Linux Command Line Mastery?

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

How long does the “Variables, Input, and Output” 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 Linux Command Line Mastery lesson?

Yes. Every Linux Command Line Mastery 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. Variables, Input, and Output
  2. Conditional Statements: `if`, `else`, `case`
  3. Loops: `for`, `while`, `until`
  4. Functions and Arguments in Shell Scripts
← Back to Linux Command Line Mastery