DEV Community

Amanda Igwe
Amanda Igwe

Posted on

Day 28/ 30 Days of Linux Mastery: Variables and Inputs in Shell Scripting

Table of Contents


Introduction

Welcome back to Day 28 of this practical Linux challenge! Part 2 of our beginner shell scripting series for RHEL 9.

Shell scripts become truly useful when they can store data, accept user input, and respond accordingly.

In the previous article, you wrote your first script. Now, let’s make that script interactive by using variables and user inputs.


What are Variables?

A variable is like a labeled box where you can store information for your script to use later.

name="Amanda Igwe"
Enter fullscreen mode Exit fullscreen mode

Here, name is the variable, and "Alex" is the value stored in it.
No spaces between variable name, =, and value!


How to Use Variables

  • To create a variable
greeting="Hello!"   - # remember no spaces
Enter fullscreen mode Exit fullscreen mode
  • To use the variable, add a $ before it
echo "$greeting"   # echo means print the variable in greeting

# Output:

Hello! 
Enter fullscreen mode Exit fullscreen mode

We have two types of variables;

  • Local Variables - This refers to the variables that run in your current shell. If you open another shell, it won't work. For example the variable greeting="Hello!" will only work in our current terminal no where else.
# local variable example

myname="Amanda"

echo $myname  
Enter fullscreen mode Exit fullscreen mode
  • Environment Variables - This refers to the variables that is inherited by any child processes or scripts you run from your current session. It can only stop running if the system is rebooted or terminal is closed. (This can also be made permanent by adding it to your .bashrc or .bash_profile file)
# Environment variable example

export myname="Amanda"

echo $myname 

# to check the environment variables 
env
Enter fullscreen mode Exit fullscreen mode

Taking Input from Users with read

Now that you have created your variables, your script will in one way or the other request details or information from your user like their name, age, phone number, home address, etc. To get this input we use read.

echo "What is your name?"    # this prints the question
read name                    # this collects the user's responses
echo "Hello, $name!"         # this prints the user's response inside the variable name
Enter fullscreen mode Exit fullscreen mode

Real-World Scenario: Shell Scripting

Let’s write a script that asks for your name and greets you with the date.

  • Open your terminal, create a file and this line into it
vim greet.sh   # note that script files end with a .sh

#!/bin/bash

echo "What is your name?"
read username

echo "Hello, $username! Welcome to Amanda's 30 Days Linux Challenge"

echo "Today is: $(date)"

# Save and exit using:wq!
Enter fullscreen mode Exit fullscreen mode

s1 description

  • Now, give the script permission to run
chmod +x greet.sh     # this enables it to be executable
Enter fullscreen mode Exit fullscreen mode
  • Verify the file has the execute permissions
ls -ltr greet.sh
Enter fullscreen mode Exit fullscreen mode
  • Run the script on your terminal
./greet.sh
Enter fullscreen mode Exit fullscreen mode

s3 description


Conclusion

By learning how to use variables, collect user input with read, and understand the difference between local and environment variables, you are no longer just running commands; you are interacting with your scripts and making them smarter.

In the next article, we will explore logic-making decisions using if, else, and conditions inside your script.

If this is helpful to you, feel free to bookmark, comment, like and follow me for Day 29!


Let's Connect!

If you want to connect or share your journey, feel free to reach out on LinkedIn.
I am always happy to learn and build with others in the tech space.

#30DaysLinuxChallenge #Redhat#RHCSA #RHCE #CloudWhistler #Linux #Rhel #Ansible #Vim #CloudComputing #DevOps #LinuxAutomation #IaC #SysAdmin#CloudEngineer

Top comments (0)