An Insight of Linux Variables in Shell Scripting Language


Introduction

Linux is one of most popular operating systems in world, with millions of users worldwide. One of many features that make Linux such a powerful tool is ability to use variables in shell scripting language. Linux variables are placeholders used to store and manipulate data in scripts. In this article, we will take a closer look at Linux variables and how they can be used in shell scripting language.

What are Linux variables?

Linux variables are similar to variables used in other programming languages. They are used to store data and can be accessed and manipulated as needed. In shell scripting language, variables are typically denoted by a dollar sign followed by variable name. For example, if you wanted to create a variable called "name", you would use following syntax −

name="John"

In this example, variable "name" is assigned value "John". This value can be accessed and manipulated throughout script using variable name.

Types of Linux variables

There are several different types of variables that can be used in Linux shell scripting language. Some of most common types include −

  • Environment variables

  • Local variables

  • Shell variables

  • Positional parameters

  • Special variables

Let's take a closer look at each of these types of variables.

Environment variables

Environment variables are variables that are set by operating system or user and are available to all processes running on system. They are typically used to provide information about system, such as user's home directory or path to important system files.

Some common environment variables include −

  • HOME − user's home directory

  • PATH − a list of directories to search for executable files

  • USER − current user's username

  • SHELL − path to user's default shell

  • PWD − current working directory

To access value of an environment variable, you can simply reference it using dollar sign syntax. For example, to print value of HOME variable, you would use following command −

echo $HOME

This would output path to current user's home directory.

Local variables

Local variables are variables that are defined within a specific script or function and are only available within that context. They are typically used to store temporary data that is needed for a specific task.

To define a local variable, you can use same syntax as for environment variables, but you do not need to use export command. For example, to define a local variable called "count" with a value of 10, you would use following command −

count=10

Shell variables

Shell variables are variables that are set by shell itself and are available to all scripts and processes running within that shell. They are typically used to provide information about shell environment, such as name of shell or current user's username.

Some common shell variables include −

  • $0 − name of current script or shell

  • $1, $2, $3, ... − positional parameters passed to script or shell

  • $# − number of positional parameters passed to script or shell

  • $* − all positional parameters as a single string

  • $@ − all positional parameters as separate strings

Positional parameters

Positional parameters are variables that are set based on arguments passed to a script or function when it is called. They are typically used to provide input to a script or function and can be accessed using $1, $2, $3, ... syntax.

For example, if you had a script called "myscript.sh" that took two arguments, you could access those arguments using following syntax −

#!/bin/bash

echo "The first argument is: $1"
echo "The second argument is: $2"

If you called this script with command "myscript.sh hello world", it would output −

The first argument is: hello
The second argument is: world

Special variables

Special variables are variables that have special meanings in shell scripting language. They are typically used to provide additional information about script or shell environment.

Some common special variables include −

  • $? − exit status of last command executed

  • $! − process ID of last command executed in background

  • $_ − last argument of previous command

  • $0 − name of current script or shell

Using variables in shell scripting language

Now that we have a better understanding of different types of variables in shell scripting language, let's take a look at how they can be used in practice.

One common use case for variables is to store data that will be used multiple times in a script. For example, you might want to prompt user for their name and store it in a variable so that you can use it throughout rest of script −

#!/bin/bash

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

In this example, "read" command prompts user to enter their name and stores it in a variable called "name". script then uses variable to print a personalized greeting to user.

Variables can also be used to store output of a command for later use. For example, you might want to list all files in a directory and then perform some action on each file. You could do this using a loop and a variable to store filename −

#!/bin/bash

for file in $(ls /path/to/directory); do
  echo "Processing $file"
  # perform some action on $file
done

In this example, "for" loop iterates over each file in directory and stores filename in a variable called "file". script then uses variable to print a message and perform some action on each file.

Additional Features

In addition to basic syntax and usage of variables in shell scripting language, there are some additional features and best practices that can help you make most of this powerful tool.

Variable scope

One important consideration when working with variables in shell scripting language is variable scope. As we discussed earlier, local variables are only available within specific script or function in which they are defined. This can be useful for storing temporary data that is only needed for a specific task.

However, if you need to access a variable from multiple scripts or functions, you may want to consider using an environment variable or a shell variable instead. These types of variables are available to all processes running within same shell environment and can be used to share data between scripts and functions.

Variable expansion

Another useful feature of variables in shell scripting language is variable expansion. This allows you to use value of a variable in a command or expression without having to explicitly reference variable name.

For example, you could use variable expansion to create a new directory with a name based on current date −

#!/bin/bash

today=$(date +%Y-%m-%d)
mkdir "backup_$today"

In this example, "date" command is used to generate a date string in format "YYYY-MM-DD", which is stored in a variable called "today". script then uses variable expansion to include value of "today" variable in directory name.

Variable naming conventions

When working with variables in shell scripting language, it is a good idea to follow some basic naming conventions to make your code more readable and easier to maintain. Some common conventions include −

  • Use descriptive names − choose variable names that accurately describe data they represent, such as "name" or "count".

  • Use underscores to separate words − this can make variable names more readable, such as "backup_directory" instead of "backupdirectory".

  • Use all uppercase for environment variables − this can help to distinguish environment variables from local variables and shell variables.

  • Use lowercase for local and shell variables − this can make it easier to distinguish these types of variables from environment variables.

By following these conventions, you can create code that is more consistent and easier to understand, even for other developers who may be working on your scripts in future.

Conclusion

Linux variables are a powerful tool for shell scripting language that allow you to store and manipulate data as needed. There are several different types of variables that can be used, including environment variables, local variables, shell variables, positional parameters, and special variables. By using variables in your scripts, you can make your code more efficient, more readable, and more maintainable.

Updated on: 20-Apr-2023

86 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements