Writing Your First Linux Script: A Beginner’s Complete Guide

Learn how to write your first Linux script with this comprehensive guide for beginners. Covers basic syntax, permissions, execution, and best practices in shell scripting.
code
linux
Author

Steven P. Sanderson II, MPH

Published

February 21, 2025

Keywords

Programming, writing linux script, shell script tutorial, bash scripting, linux script basics, how to write shell scripts, shell script permissions, bash script examples, ilnux script location, shell scripting commands, bash script syntax, how to make a linux script executable, where to store shell scripts in linux, how to write your first bash script for beginners, how to add shell script to linux path, how to debug basic shell scripts in linux

Author’s Note: As I learn and write this series, I encourage readers to point out any errors in the comments section. Learning is a journey we’re taking together!

Introduction

Writing your first Linux script can feel like learning a new language - exciting yet challenging. This comprehensive guide will walk you through creating, understanding, and executing your first Linux shell script, setting you on the path to Linux scripting mastery.

What is a Shell Script?

A shell script is essentially a text file containing a series of commands that the shell reads and executes sequentially. Think of it as writing down a set of instructions that you’d normally type into the command line, but now the computer can run them automatically.

Prerequisites for Writing Linux Scripts

Before we begin, ensure you have:

  • A Linux operating system installed
  • Basic familiarity with command line operations
  • A text editor (vim, gedit, or kate)
  • Terminal access

Setting Up Your Development Environment

Choosing the Right Text Editor

While any text editor will work, it’s recommended to use one with:

  • Syntax highlighting
  • Auto-indentation
  • Line numbering

Pro Tip: If using vim, add these lines to your ~/.vimrc file for optimal script writing:

syntax on
set hlsearch
set tabstop=4
set autoindent

Creating Your First Script

Let’s create a classic “Hello World” script to understand the basics:

  1. Open your text editor
  2. Enter the following code:
#!/bin/bash
# This is our first script.
echo 'Hello World!'

Understanding Each Line:

  1. #!/bin/bash - This is the shebang line, telling the system to use the bash interpreter
  2. # This is our first script - A comment explaining the script
  3. echo 'Hello World!' - The actual command to execute

Making Your Script Executable

After creating your script, you need to make it executable. Here’s how:

  1. Save the file as hello_world
  2. Open terminal and navigate to the script’s location
  3. Make it executable with:
chmod 755 hello_world

Permission Options:

  • 755 - Everyone can execute
  • 700 - Only owner can execute

Script Location and Path

Where to Store Your Scripts

The best locations for your scripts are:

  • Personal use: ~/bin
  • System-wide use: /usr/local/bin
  • Admin scripts: /usr/local/sbin

Setting Up Your Path

To run scripts from anywhere, add your script directory to PATH:

export PATH=~/bin:"$PATH"

Add this line to your .bashrc file for persistence.

Best Practices for Script Writing

1. Clear Commenting

# This is a comment explaining what the script does
echo 'Hello World!' # This explains this specific line

2. Proper Formatting

Use indentation and line continuation for readability:

find playground \
    -type f \
    -name "*.txt" \
    -exec chmod 600 '{}' ';'

3. Use Descriptive Names

  • Choose clear, meaningful names for your scripts
  • Avoid spaces in filenames
  • Use underscores for separation

Your Turn!

Try creating this script on your own:

  1. Create a new script called system_info
  2. Make it display:
    • Current date
    • Hostname
    • System uptime

Solution:

#!/bin/bash
# Script to display system information

echo "Date: $(date)"
echo "Hostname: $(hostname)"
echo "Uptime: $(uptime)"

Quick Takeaways

  • Scripts are text files containing shell commands
  • Always start with a shebang (#!/bin/bash)
  • Must be made executable using chmod
  • Store in appropriate directories
  • Use proper formatting and comments

FAQs

  1. Q: Why doesn’t my script run when I type its name? A: Ensure it’s executable and in a directory listed in your PATH.

  2. Q: What’s the difference between 755 and 700 permissions? A: 755 allows everyone to execute; 700 restricts execution to the owner.

  3. Q: Do I need the .sh extension for shell scripts? A: No, it’s optional in Linux but can help identify file types.

  4. Q: Can I write scripts without the shebang line? A: Yes, but it’s not recommended as it helps specify the correct interpreter.

  5. Q: How do I debug my shell scripts? A: Use bash -x script_name to run in debug mode.

Conclusion

Writing your first Linux script is an important step in your journey to Linux mastery. Remember to start simple, follow best practices, and gradually build complexity as you learn. Don’t forget to experiment and create your own scripts to automate daily tasks.

References

  1. Linux Command: Writing Shell Scripts - A comprehensive guide by William Shotts
  2. Linux Training Academy: Writing Your First Linux Script - Practical tutorial with examples
  3. GeeksforGeeks: Shell Script Examples - Collection of practical shell scripting examples

Engage!

Found this helpful? Share it with fellow Linux enthusiasts and let me know in the comments if you have any questions or suggestions!


Happy Coding! 🚀

First Script

You can connect with me at any one of the below:

Telegram Channel here: https://t.me/steveondata

LinkedIn Network here: https://www.linkedin.com/in/spsanderson/

Mastadon Social here: https://mstdn.social/@stevensanderson

RStats Network here: https://rstats.me/@spsanderson

GitHub Network here: https://github.com/spsanderson

Bluesky Network here: https://bsky.app/profile/spsanderson.com

My Book: Extending Excel with Python and R here: https://packt.link/oTyZJ

You.com Referral Link: https://you.com/join/EHSLDTL6