Skip to main content

Command Palette

Search for a command to run...

Shell Scripting - Part 2

Published
3 min read

Environmental Variables

An environmental variable in shell scripting is a variable that holds information that can be accessed by programs or commands running in the shell environment. These variables are set by the shell itself or by user-defined scripts or programs and can be used to store various types of information, such as system configuration, user preferences, or runtime data.

Some common environmental variables in shell scripting include:

  • PATH: specifies the directories where executable programs are located.

  • HOME: the user's home directory.

  • SHELL: the user's default shell.

  • USER: the username of the current user.

  • TERM: the terminal type being used.

PS1: the primary prompt string used in the shell.

Environmental variables can be accessed and modified using the shell's built-in commands or through user-defined scripts and programs. They are often used to customize the behavior of shell scripts and to provide information to other programs and processes running in the shell environment.

Note: Use .bashrc file to add any other variables which will survive even after a reboot.

Alias

You can create your customized commands using Alias, this concept is very useful when we are dealing with lengthy commands. Below is the demonstration of using Alias in real-time.

For Example, to list our all vpcs that are created in the AWS account using the below command.

But, it is hard to remember this command every time you want to pull the list of vpcs. So, we use alias command for storing this command in a variable and that variable would be saved in the .bashrc file.

Another example is to pull out the list of all instances created in a region, I added the below command to the .bashrc file. After adding the alias entry you need to either run the source .bashrc command or log out from the root and log in again.

Special Variables

In shell scripting, several special variables provide useful information and control over the execution of scripts. Here's an explanation of $?, $#, $@, and $*:

  1. $?: This variable holds the exit status or return code of the last executed command. After executing a command, you can access its exit status using $?. Conventionally, a zero value (0) indicates successful execution, while non-zero values indicate errors or specific conditions.

     ls -l
     echo "Exit status: $?"
    

    In the above example, the ls -l command is executed, and then the exit status is printed using echo "$?".

  2. $#: This variable holds the number of command-line arguments passed to a shell script or function. It is particularly useful when you want to determine the number of arguments provided. For example:

#!/bin/bash
echo "Number of arguments: $#"

In the above script, $# is used to print the number of arguments passed when executing the script.

  1. $@: This variable represents all the command-line arguments passed to a shell script or function as separate parameters. Each argument is treated as a separate entity. You can access them individually using positional parameters like $1, $2, etc., or you can use $@ to represent all the arguments as an array-like structure. For example:
#!/bin/bash
echo "All arguments: $@"

In the above script, $@ is used to print all the arguments passed when executing the script.

  1. $*: This variable represents all the command-line arguments passed to a shell script or function as a single string. It concatenates all the arguments into a single string, with spaces separating each argument. For example:
#!/bin/bash
echo "All arguments: $*"

In the above script, $* is used to print all the arguments passed when executing the script as a single string.

Note that when using $@ or $*, it is generally recommended to enclose them in double quotes ("$@" or "$*") to preserve argument boundaries, especially when arguments contain spaces or special characters.