Skip to content

Latest commit

 

History

History
63 lines (40 loc) · 1.62 KB

2.md

File metadata and controls

63 lines (40 loc) · 1.62 KB

Chapter 1: Introduction to shell scripting 🚀

2 - Creating your first script

Writing and Executing Your First Shell Script in Linux

Shell scripting is a powerful tool for automating tasks and executing commands in Linux. Here's a beginner-friendly guide to writing and executing your first shell script.

Writing Your Script

  1. Choose a Text Editor: Open your preferred text editor. Common choices include nano, vim, or gedit.

  2. Create a New File: Start a new file by typing:

    nano my_script.sh

    Replace my_script.sh with your desired file name.

  3. Add SheBang: Begin your script with a SheBang line to specify the shell interpreter. For bash, use:

    #!/bin/bash
  4. Write Your Commands: Add the commands you want your script to execute. For example:

    #!/bin/bash
    
    echo "Hello, world!"
  5. Save and Exit: Press Ctrl + X to exit nano. Confirm to save changes by typing Y, then press Enter.

Making Your Script Executable

  1. Set Permissions: Make your script executable using the chmod command:
    chmod +x my_script.sh

Executing Your Script

  1. Run Your Script: Execute your script using the ./ notation followed by the script name:

    ./my_script.sh
  2. View Output: Your script's output will be displayed in the terminal.

Example

Here's an example of a simple shell script:

#!/bin/bash

echo "Hello, world!"

⬅️Previous-----------Next ➡️

Table of contents 🚀