How To Run Sh Script In Terminal

How To Run Sh Script In Terminal

Reading time1 min
#Terminal#Scripting#Shell#Shellscript#Linux#Bash

Mastering the Terminal: How to Run .sh Scripts Efficiently and Safely

Forget complex GUIs and third-party tools; the terminal offers the fastest, most transparent way to execute shell scripts once you know the few critical commands and safety checks. This post will cut through the noise and deliver practical, no-nonsense guidance to harnessing shell scripts like a pro.


Why Run Shell Scripts from the Terminal?

Shell scripts (.sh files) are powerful tools that automate repetitive tasks, configure environments, or troubleshoot issues—all directly from your Unix-like terminal (Linux, macOS, BSD). Knowing how to run these scripts efficiently—and importantly, safely—can save hours and increase your productivity. If you’re new to shell scripting or just want a refresher on execution basics, you’re in the right place.


Step 1: Prepare Your Script for Execution

Before running any .sh script, ensure it is ready:

  1. Check the script’s contents:

    Always review what a script does before running it, especially if downloaded from the internet or shared by someone else. Open it in any text editor:

    nano myscript.sh
    # or
    cat myscript.sh
    
  2. Make sure it has the right "shebang" line:

    This line at the top specifies which shell interpreter runs the script.

    Example:

    #!/bin/bash
    

    or

    #!/usr/bin/env bash
    
  3. Give execute permissions:

    To run your script as a program, it needs execution rights:

    chmod +x myscript.sh
    

Step 2: Running Your Script in Terminal

There are two common ways to run .sh scripts:

Option A: Using ./ notation (direct execution)

If your current directory contains myscript.sh and it has executable permission (see step 1), run:

./myscript.sh

This executes the script using its shebang line.

Note: ./ means "in current directory". Without ./, just typing myscript.sh usually won’t work because current dir isn’t in your PATH by default.

Option B: By invoking an interpreter explicitly

You can run any shell script by calling its interpreter command explicitly:

bash myscript.sh

or

sh myscript.sh

This works regardless of executable permissions but ignores shebang lines.


Quick Example

Suppose you write a script named greet.sh like this:

#!/bin/bash
echo "Hello, World!"

Make it executable:

chmod +x greet.sh

Run it:

./greet.sh

Output should be:

Hello, World!

Alternatively,

bash greet.sh

will produce the same output even if you don’t make it executable.


Step 3: Additional Tips for Safety and Efficiency

  • Never run scripts blindly, especially those obtained online. Always read and understand them first.
  • Use set -e at the start of your script to make it exit immediately on errors.
  • Run scripts with minimum required privileges — avoid running as root unless absolutely necessary.
  • For scripts that modify files or system settings, create backups before running them.
  • Use absolute paths inside scripts when possible to avoid confusion.

Bonus: Passing Arguments to Your Script

Scripts can accept arguments just like commands do. Inside your script, $1, $2, etc., represent the first, second arguments respectively.

Example backup.sh:

#!/bin/bash

echo "Backing up directory: $1"
tar -czf backup.tar.gz "$1"
echo "Backup complete."

Run as:

./backup.sh /path/to/folder

Wrapping Up

Mastering .sh script execution from your terminal is straightforward:

  • Prepare your script (check shebang and permissions).
  • Run using ./scriptname.sh or bash scriptname.sh.
  • Practice safe habits before executing anyone else’s code.
  • Pass arguments to customize behavior.

Once comfortable with these steps, you’ll be automating like a pro—making your command line work for you instead of against you!


Have any favorite shell scripts that speed up your workflow? Share them below! And if you want more deep dives into terminal productivity tips, stay tuned.