Sitemap
PratikMurlidharYadav

PratikMurlidharYadav

SHELL SCRIPTING-Day4

Types Of Variables :

3 min readMay 31, 2023

In shell scripting, there are two main types of variables: user-defined variables and system-defined variables.

User-Defined Variables:

These variables are defined by the script author to store custom data and values specific to the script. Users can assign values to these variables according to their requirements. Here’s an example of user-defined variables:

#!/bin/bash

# User-defined variables
name="John"
age=25
favoriteColor="blue"

In the above example, name, age, and favoriteColor are user-defined variables that store the name, age, and favorite color of a person.

System-Defined Variables:

System-defined variables are predefined by the shell or the operating system. They provide information about the environment, system configuration, and command-line arguments. Here are some common examples of system-defined variables:

#!/bin/bash

# System-defined variables
echo "Script Name: $0"
echo "First Argument: $1"
echo "Second Argument: $2"
echo "Number of Arguments: $#"
echo "Exit Status of Last Command: $?"

Here are some commonly used system-defined variables in shell scripts:

  1. $0: This variable holds the name of the script or the shell itself.
  2. $1, $2, $3, …: These variables represent the command-line arguments passed to the script. $1 corresponds to the first argument, $2 to the second, and so on.
  3. $@: This variable represents all the command-line arguments passed to the script as a list.
  4. $#: This variable stores the number of command-line arguments passed to the script.
  5. $?: After executing a command, this variable contains the exit status of the most recently executed command. A value of 0 usually indicates success, while non-zero values represent various errors or failures.
  6. $$: This variable holds the process ID (PID) of the current shell or script.
  7. $USER: It contains the username of the user running the script.
  8. $HOME: This variable stores the home directory path of the current user.
  9. $PWD: It holds the current working directory.
  10. $PATH: This variable contains a colon-separated list of directories where the shell searches for executable programs.
  11. $SHELL: It represents the path to the default shell interpreter.

Flavors of Variables :

  1. String variables:

These variables store text or string values and alpha-numric value. You can assign values enclosed in single quotes (‘’) or double quotes (“”) to string variables. For example:

name='John'
message="Hello, $name!"
username="pratik123"

2. Integer variables:

Get PratikMurlidharYadav’s stories in your inbox

Join Medium for free to get updates from this writer.

Shell scripting does not have native support for integers, but you can work with numeric values as strings. You can use tools like arithmetic expansion or external commands like expr or bc to perform arithmetic operations on these variables. For example.

num1="10"
num2="5"
result=$(($num1 + $num2))

3. Array variables:

Arrays allow you to store multiple values in a single variable. You can define an array and access its elements using index numbers. For example:

fruits=("apple" "banana" "orange")
echo "First fruit: ${fruits[0]}"

4. Environment variables:

Shell scripting provides access to various environment variables, which are predefined by the system or the user. These variables contain information about the environment in which the script is running. Examples of common environment variables include:

  • $HOME: Represents the user's home directory.
  • $PATH: Specifies the directories to search for executable programs.
  • $USER: Stores the username of the current user.

You can access these variables within your script to retrieve information about the environment.

5. Positional parameters:

These variables hold command-line arguments passed to the shell script when it is executed. The first argument is stored in $1, the second in $2, and so on. For example:

echo "First argument: $1"

6. Special variables:

Shell scripting provides special variables with predefined meanings. Some commonly used special variables include:

  • $0: Represents the name of the script itself.
  • $#: Stores the number of arguments passed to the script.
  • $?: Holds the exit status of the last command executed.
  • $$: Represents the process ID (PID) of the current script.

These special variables can provide useful information and control within your shell scripts.

These are the basic types of variables in shell scripting. Understanding and utilizing these variables can help you perform a wide range of tasks within your shell scripts.

thanks for reading!

--

--