Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

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

Posted on

     

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, likebash,sh,zsh, orksh. 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, thebash 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 thetouch command:

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

Step 3:Writing a Basic Shell Script

  1. Open the Script in an Editor:
   nano my_first_script.sh
Enter fullscreen modeExit 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 modeExit fullscreen mode

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

  1. Add Commands:Begin by adding some basic commands:
#!/bin/bashecho"Hello, World!"
Enter fullscreen modeExit fullscreen mode
  • echo is used to print text to the terminal.
  1. Save and Exit:Innano, pressCTRL + X, thenY, and hitENTER 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 modeExit fullscreen mode

You should see the output:

   Hello, World!
Enter fullscreen modeExit fullscreen mode

Step 5:Adding Logic and Functionality

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

  1. Variables:
#!/bin/bashname="John"echo"Hello,$name!"
Enter fullscreen modeExit fullscreen mode
  1. Conditionals:
#!/bin/bashage=25if[$age-ge 18];thenecho"You are an adult."elseecho"You are a minor."fi
Enter fullscreen modeExit fullscreen mode
  1. Loops:
#!/bin/bashforiin1 2 3 4 5;doecho"Iteration:$i"done
Enter fullscreen modeExit fullscreen mode
  1. Functions:
#!/bin/bash   greet(){echo"Hello,$1!"}   greet"Alice"   greet"Bob"
Enter fullscreen modeExit fullscreen mode

Step 6:Handling Input and Output

  1. Read User Input:
#!/bin/bashecho"Enter your name:"readnameecho"Welcome,$name!"
Enter fullscreen modeExit fullscreen mode
  1. Redirecting Output:Save command output to a file:
echo"This is a log file."> log.txt
Enter fullscreen modeExit 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 modeExit fullscreen mode
  1. Handling Arguments:Use positional parameters to handle script arguments:
#!/bin/bashecho"The first argument is:$1"echo"The second argument is:$2"
Enter fullscreen modeExit fullscreen mode

Run with:

   ./my_first_script.sh arg1 arg2
Enter fullscreen modeExit 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 modeExit fullscreen mode
  1. Comment Your Code:Use comments (#) to explain your code:
# This is a commentecho"Hello, World!"# Print Hello
Enter fullscreen modeExit fullscreen mode
  1. Error Handling:Use|| and&& for simple error handling:
mkdirnew_directory&&cdnew_directory||echo"Failed to create or navigate to directory."
Enter fullscreen modeExit fullscreen mode

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

As a dedicated Backend Engineer with six years of experience, I specialize in developing robust and scalable backend solutions. My technical expertise spans PHP, Laravel, MySQL, C++, AWS, etc.
  • Location
    Dhaka, Bangladesh
  • Education
    B.sc in CSE
  • Work
    Senior Software Engineer at Siliconorchard Ltd.
  • Joined

More fromMd Abu Musa

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp