Day-1: Introduction to Shell Scripting: Basic Syntax, Shebang, Comments, and Variables

Day-1: Introduction to Shell Scripting: Basic Syntax, Shebang, Comments, and Variables

1. what is shell scripting?

Shell scripting is writing commands in a file to automate tasks in a shell (command-line interface). It allows you to execute multiple commands in sequence to manage systems, automate processes, and perform repetitive tasks efficiently. Scripts are usually written in languages like Bash and saved with a .sh extension.

2. Shebang (#!)

The shebang (#!) is the first line of a shell script that tells the system what interpreter to use for the script.

#!/bin/bash

This line tells the system to use /bin/bash as the shell interpreter for the script. It's important to have this at the start of every script to ensure the script runs with the correct shell.

3. Comments

In shell scripts, comments are used to explain the code and make it more readable. Anything following a # in a line is considered a comment and is ignored by the shell.

Example:

#This is a comment
echo "hello, devops gang" # This prints 'Hello, World!' to the terminal

4. Variables

Variables in shell scripts are used to store data. You don't need to declare a variable type; just assign a value to a variable, and it can hold a string, number, or any other data type.

  • Assignment: No spaces around the equal sign.

  • Usage: Use $ to access the value of the variable.

Example:

#!/bin/bash

#Assigning a value to a variable
gang="pipeline gang"

#Using the variable
echo $gang # This will print 'pipeline gang'

5. Variable Types

Variables in shell scripts are typically untyped, but you can define them as:

  • Strings: Assign text to a variable.

  • Numbers: Directly assign numeric values.

  • Arrays: (Covered later in the challenge).

#!/bin/bash

#String variable
name="Birendra"

#Numeric variable
age=25

#Using the variables
echo "My name is $name, and I am $age years old."

6. Basic Variable Substitution

To insert the value of a variable into a string, use double quotes and $variable_name.

#!/bin/bash

#Variable for name
name="Birendra"

#Using the variable in a string
echo "Hello, $name!" # Output: Hello, Birendra!

Conclusion:

Today, you learned the basics of shell scripting. You discovered how to use the shebang (#!) to specify which shell to run your script with, like /bin/bash. You also learned about comments (#), which help explain your code, and variables, which store data you can use in your script. You now know how to create variables and access their values.

If you have any questions or face issues, feel free to drop a comment below or mail me at !