Linux Shell Scripting : Functions and Variables

Linux Shell Scripting : Functions and Variables

What is Shell Scripting?

Shell scripting refers to writing scripts or programs that are interpreted and executed by a shell, which is a command-line interpreter for operating systems like Unix, Linux, and macOS. The shell is an interface between the user and the operating system, allowing users to execute commands and perform various tasks.

A shell script is a text file containing a series of commands written in a scripting language specific to the shell being used, such as Bash (Bourne Again SHell) or C Shell (csh). These scripts can automate repetitive tasks, perform system administration tasks, or combine multiple commands into a single script for more complex operations.

Functions

In shell scripting, functions are a way to group a set of commands together and give them a name. They allow you to define reusable pieces of code that can be called multiple times within a script or from other scripts. Functions help in organizing and modularizing your code, making it easier to read, understand, and maintain.

  • To call a function, you simply use its name followed by parentheses

  • You can also pass parameters to functions. Here's an example of a function that takes two parameters

  • You can call this function by providing the required arguments

  • Functions can also return values using the return statement

  • To capture the returned value, you can assign the function call to a variable

  • These are the basic concepts of using functions in shell scripting. Functions can be powerful tools for code organization and reusability, enabling you to write more maintainable scripts.

Creating Shell Script

  • Create a file using the command touch.

  • Use the file extension (.sh).

  • To edit the file use the command vi with file name.

  • Write the shebang at the top.

  • Define the name of the function.

  • Write the condition which you want to perform in the function and return the result of the function.

  • Pass the Parameters for the function.

  • Use the bash command with the file name to Run the Shell Script.

  • Below you can see the results.

Variables

In Linux shell scripting, variables are used to store and manipulate data. They provide a way to hold values that can be referenced and modified throughout the script. Variables are useful for storing user input, command outputs, and intermediate results.

Local Variable

In Linux shell scripting, local variables are variables that are limited in scope to a specific block of code, typically within a function. Local variables are only accessible within the block of code where they are defined and are not visible outside of that scope.

Example

  • To declare a local variable within a function, you use the local keyword followed by the variable name and its value:

  • function_name() { local variable_name=value # ... }

  • Here's an example that demonstrates the usage of local variables within a function:

  • print_message() { local message="Hello, World!" echo $message } print_message

Global Variable

In Linux shell scripting, global variables are variables that are accessible from any part of the script, including functions, and their values can be accessed and modified throughout the script. Unlike local variables, global variables have a global scope and can be used across different blocks of code within the script.

Example

Both in One Example

  • Both the Variables are used in this example.

  • The var2 which is outside the function is a global variable.

  • The var2 which is inside the function is a local variable.

  • When the script will Run the var2 inside the function will pick the value of a local variable.

  • When the script will Run the var2 outside the function will pick the value of a global variable.

  • You can see the results below.