Skip to content

Click on each book below to review & buy on Amazon.

As an Amazon Associate, I earn from qualifying purchases.

LPI Linux Essentials Exam 010-160 - Topic 2.1 - Variables

Variables enable you to store and manipulate data. Understanding how to work with variables is essential for creating dynamic and reusable code in Linux. This guide will introduce you to variables, their usage, and best practices.

What are Variables?

Variables are used to store data that can be accessed and manipulated. They provide a way to assign values to names and use those values in various operations. Variables can hold different types of data, such as strings, numbers, or even command outputs.

Variable Naming and Assignment

Here are some key points to remember when working with variables:

  • Naming conventions: Variables are case-sensitive and can consist of letters, digits, and underscores. They must start with a letter or an underscore.
  • Assignment: To assign a value to a variable, use the syntax variable_name=value. No spaces are allowed around the equal sign.

    greeting="Hello, World!"

Variable Usage and Expansion

Once you've assigned values to variables, you can access and expand their contents in various ways:

  • Variable expansion: To access the value stored in a variable, prefix the variable name with a dollar sign $. This is called variable expansion.

    Exercise: Declare a variable and display its value to screen

    echo "Hello, $name!"

    The output should read as:

    Hello, John!
  • Command substitution: You can also assign the output of a command to a variable using command substitution, denoted by $(command). A typical use case of this is to get a time/date stamp for use in file names.

    Exercise: Declare a variable that contains the current date and print that to screen by expanding the date variable:

    current_date=$(date +%Y-%m-%d)
    echo "Today's date is $current_date."

    The output should resemble:

    Today's date is 2023-10-10.

Using Variables

Variables can be used and set in many ways. Here are some examples of how variables can be utilized:

  • User input: You can prompt the user for input and store it in a variable for further processing using the read command.

    Exercise: Set the name variable when prompted and print its value to screen:

    The following read command will display a prompt to screen (Enter your name:) where you can enter your name:

    read -p "Enter your name: " name
    echo "Welcome, $name!"

    The output should display your name as entered into the read prompt, for example:

    Welcome, John Doe!
  • Mathematical operations: Variables can be used in mathematical calculations.

    Exercise: Perform a simple additional calculation:

    sum=$((x + y))
    echo "The sum of $x and $y is $sum."

    The calculation should have taken place and display the correct sum to screen:

    The sum of 5 and 10 is 15.

Support DTV Linux

Click on each book below to review & buy on Amazon. As an Amazon Associate, I earn from qualifying purchases.

NordVPN ®: Elevate your online privacy and security. Grab our Special Offer to safeguard your data on public Wi-Fi and secure your devices. I may earn a commission on purchases made through this link.