DEV Community

Cover image for Step-by-Step Guide to Start Shell Scripting.
Md Abu Musa
Md Abu Musa

Posted on

7 1 1

Step-by-Step Guide to Start Shell Scripting.

Shell scripting is a way to automate repetitive tasks, manage system operations, and create custom utilities using a command-line interpreter, like bash, sh, zsh, or ksh. A shell script is simply a text file containing a series of commands that you would normally type into a terminal manually.

Step-by-Step Guide to Start Shell Scripting:

Step 1: Understanding Shell Scripting

  • Definition: Shell scripts are files containing sequences of shell commands to be executed in order.
  • Purpose: Automate tasks like backups, system monitoring, file manipulations, and installations.
  • Basic Shells: Common shell interpreters include:
    • bash (Bourne Again Shell)
    • sh (Bourne Shell)
    • zsh (Z Shell)
    • ksh (Korn Shell)

For beginners, the bash shell is the most widely used and recommended.


Step 2: Setting Up the Environment

  1. Choose an Editor:
    Use a text editor to create and edit shell scripts:

    • CLI Editors: nano, vim, emacs
    • GUI Editors: VS Code, Sublime Text, Atom
  2. Create a New Script File:
    Open the terminal and create a new file using the touch command:

   touch my_first_script.sh
Enter fullscreen mode Exit fullscreen mode
  1. Make the Script Executable: To execute your script, it must have executable permissions. Grant it using chmod:
   chmod +x my_first_script.sh
Enter fullscreen mode Exit fullscreen mode

Step 3: Writing a Basic Shell Script

  1. Open the Script in an Editor:
   nano my_first_script.sh
Enter fullscreen mode Exit fullscreen mode
  1. Start with a Shebang: The first line of a shell script should specify the shell to use, using the shebang (#!) syntax:
   #!/bin/bash
Enter fullscreen mode Exit fullscreen mode

This tells the system that it should use the bash shell to run the script.

  1. Add Commands: Begin by adding some basic commands:
   #!/bin/bash
   echo "Hello, World!"
Enter fullscreen mode Exit fullscreen mode
  • echo is used to print text to the terminal.
  1. Save and Exit: In nano, press CTRL + X, then Y, and hit ENTER to save and exit.

Step 4: Running Your First Shell Script

  1. Execute the Script: In the terminal, run the script by specifying its path:
   ./my_first_script.sh
Enter fullscreen mode Exit fullscreen mode

You should see the output:

   Hello, World!
Enter fullscreen mode Exit fullscreen mode

Step 5: Adding Logic and Functionality

Expand your script by adding variables, conditional statements, loops, and functions:

  1. Variables:
   #!/bin/bash
   name="John"
   echo "Hello, $name!"
Enter fullscreen mode Exit fullscreen mode
  1. Conditionals:
   #!/bin/bash
   age=25
   if [ $age -ge 18 ]; then
       echo "You are an adult."
   else
       echo "You are a minor."
   fi
Enter fullscreen mode Exit fullscreen mode
  1. Loops:
   #!/bin/bash
   for i in 1 2 3 4 5; do
       echo "Iteration: $i"
   done
Enter fullscreen mode Exit fullscreen mode
  1. Functions:
   #!/bin/bash
   greet() {
       echo "Hello, $1!"
   }

   greet "Alice"
   greet "Bob"
Enter fullscreen mode Exit fullscreen mode

Step 6: Handling Input and Output

  1. Read User Input:
   #!/bin/bash
   echo "Enter your name:"
   read name
   echo "Welcome, $name!"
Enter fullscreen mode Exit fullscreen mode
  1. Redirecting Output: Save command output to a file:
   echo "This is a log file." > log.txt
Enter fullscreen mode Exit fullscreen mode
  1. Appending to Files: Append content to an existing file:
   echo "This will be added to the log file." >> log.txt
Enter fullscreen mode Exit fullscreen mode
  1. Handling Arguments: Use positional parameters to handle script arguments:
   #!/bin/bash
   echo "The first argument is: $1"
   echo "The second argument is: $2"
Enter fullscreen mode Exit fullscreen mode

Run with:

   ./my_first_script.sh arg1 arg2
Enter fullscreen mode Exit fullscreen mode

Step 7: Debugging and Improving Your Script

  1. Debugging: Run the script with -x to see each command as it is executed:
   bash -x my_first_script.sh
Enter fullscreen mode Exit fullscreen mode
  1. Comment Your Code: Use comments (#) to explain your code:
   # This is a comment
   echo "Hello, World!"  # Print Hello
Enter fullscreen mode Exit fullscreen mode
  1. Error Handling: Use || and && for simple error handling:
   mkdir new_directory && cd new_directory || echo "Failed to create or navigate to directory."
Enter fullscreen mode Exit fullscreen mode

Reinvent your career. Join DEV.

It takes one minute and is worth it for your career.

Get started

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs