How to run sh script

Content on WhatAnswers is provided "as is" for informational purposes. While we strive for accuracy, we make no guarantees. Content is AI-assisted and should not be used as professional advice.

Last updated: April 4, 2026

Quick Answer: Execute a shell script by making it executable with chmod +x filename.sh, then run it with ./filename.sh in the terminal. Alternatively, run any shell script directly using bash filename.sh or sh filename.sh without changing permissions. Both methods work on Linux, macOS, and other Unix-based systems.

Key Facts

What It Is

A shell script is a text file containing a sequence of shell commands that execute automatically when run, automating repetitive tasks in Unix-based systems. Shell scripts act as programs that combine command-line tools, creating powerful workflows without compiling code. The .sh extension indicates shell script files, though not all shell scripts require this extension. Shell scripts interpret commands sequentially, making them ideal for system administration, backup automation, and development workflows.

Shell scripting emerged in the 1970s with the original Bourne shell (sh), developed by Stephen Bourne at Bell Labs as part of Unix Version 7. The Bash shell, created by Brian Fox in 1989 for the GNU project, became the standard default shell on Linux systems. The C shell (csh) and Korn shell (ksh) provided alternative syntax in the 1980s-1990s, each optimizing for different use cases. Modern systems typically default to Bash on Linux and Zsh on macOS, though sh remains available for POSIX compatibility.

Shell script variations include Bash scripts (#!/bin/bash), POSIX shell scripts (#!/bin/sh), Zsh scripts (#!/bin/zsh), and Korn shell scripts (#!/bin/ksh). Each variant offers slightly different syntax and features, with Bash being most commonly used. Simple scripts might contain just 2-3 commands, while complex system administration scripts can exceed 1000 lines. Thousands of enterprise systems rely on shell scripts for critical operations like database backups and log rotation.

How It Works

Shell scripts work by reading commands from a text file line-by-line and passing them to the shell interpreter for execution. When you run a script, the operating system reads the shebang (#!) line to determine which interpreter processes subsequent commands. The interpreter then executes each command sequentially, with standard output displaying results and standard error showing any errors. Variables, loops, and conditional statements provide logic control within scripts.

Consider a practical example: a system administrator at a company like Google or AWS might create a backup script that uses tar and rsync commands. The script starts with #!/bin/bash, then uses variables like BACKUP_DIR=/backups and DATE=$(date +%Y%m%d). It includes a for loop iterating through multiple directories, running tar -czf command to compress files, then rsync to upload to backup servers. Finally, it sends an email notification using mail command with the backup status. This script can automate a 30-minute manual process into a single command.

To run a shell script: first create a text file with .sh extension containing commands. Add #!/bin/bash as the first line to specify Bash as the interpreter. Save the file and make it executable using chmod +x script.sh in your terminal. Execute it by typing ./script.sh from the same directory, or specify the full path like /home/user/scripts/script.sh. Alternatively, skip the chmod step and run directly with bash script.sh or sh script.sh.

Why It Matters

Shell scripts enable system administrators to automate tasks that would otherwise require manual intervention, saving hundreds of hours annually in enterprise environments. According to 2024 DevOps surveys, 87% of IT professionals use shell scripts for automation tasks, demonstrating their continued relevance. Linux system maintenance relies heavily on cron jobs executing shell scripts at scheduled intervals for tasks like log rotation, backups, and security updates. This automation reduces human error and ensures consistency in operations.

Shell scripting applications span DevOps, system administration, embedded systems, and cloud infrastructure management at companies like Microsoft Azure, Amazon AWS, and Google Cloud. CI/CD pipelines rely on shell scripts to build, test, and deploy software across thousands of servers. Kubernetes deployments frequently use shell scripts in initialization containers. Network administration uses shell scripts for automated monitoring, firewall configuration, and traffic analysis across enterprise networks.

Future trends include Infrastructure-as-Code adoption increasing reliance on shell scripts for cloud resource provisioning and management. Containerized environments like Docker still heavily use shell scripts in Dockerfiles for image creation. Security automation increasingly uses shell scripts for vulnerability scanning and patch management across large organizations. Despite newer languages like Python gaining adoption, shell scripts remain irreplaceable for direct system command orchestration.

Common Misconceptions

Myth 1: Shell scripts require advanced programming knowledge. Reality: Basic shell scripts need only familiarity with common commands like ls, cd, cp, and echo. Beginners can create useful scripts within hours using simple syntax. Many people write their first useful shell script within days of learning basic command-line commands.

Myth 2: You must make scripts executable before running them. Reality: Running bash filename.sh or sh filename.sh executes scripts without execute permissions. Making scripts executable with chmod +x only becomes necessary when running them directly as ./filename.sh. Both approaches work identically; permission requirements depend on execution method.

Myth 3: Shell scripting is obsolete and replaced by Python. Reality: Shell scripts remain the standard for system administration and DevOps workflows where direct command execution is needed. Linux distributions include Bash by default while Python requires installation. Shell scripts integrate seamlessly with cron jobs and system startup sequences, making them indispensable for operations. Modern practices combine both: Python for complex logic and shell scripts for orchestration.

Related Questions

Q: What does chmod +x do? A: The chmod +x command adds execute permission to files, allowing them to run directly as programs. It changes file permissions from read/write only to read/write/execute (mode 755). Without this permission, you must explicitly invoke bash or sh to run the script.

Q: Can I run shell scripts on Windows? A: Yes, using Windows Subsystem for Linux (WSL), Git Bash, or MSYS2 environments on Windows 10/11. Native Windows PowerShell provides similar functionality with different syntax. For full compatibility, Linux or macOS systems natively support shell scripts without additional software.

Q: How do I pass arguments to shell scripts? A: Use positional parameters $1, $2, $3, etc., where $1 is the first argument after the script name. For example, ./script.sh arg1 arg2 makes arg1 available as $1 and arg2 as $2 within the script. Use $@ to reference all arguments and $# to count total arguments.

Related Questions

What does the shebang (#!) line do and why is it important?

The shebang (#!) is a special comment that tells the operating system which interpreter to use when executing the script, with #!/bin/bash specifying Bash shell and #!/bin/sh specifying the standard POSIX shell. Without a shebang, the system defaults to the user's login shell, which may not be the intended shell, causing compatibility issues or unexpected behavior. The shebang path must point to a valid shell interpreter; incorrect paths result in 'command not found' errors, so verifying the correct path with which bash or which sh is essential.

Why do I get 'permission denied' when trying to run a shell script?

The 'permission denied' error occurs when the script file lacks execute permission for the user attempting to run it, which is set using chmod +x scriptname.sh to add execute permission. You can verify permissions using ls -l scriptname.sh, where the first character should be 'd' for directories and '-' for files, followed by permission settings including 'x' for executable. Running bash scriptname.sh or sh scriptname.sh bypasses the need for execute permission since you're explicitly invoking the shell interpreter.

How can I pass arguments to a shell script when running it?

Arguments are passed after the script name using spaces: ./scriptname.sh arg1 arg2 arg3, which are then accessible inside the script as $1, $2, $3 respectively, with $0 containing the script name. The special variable $# contains the total number of arguments passed, and $@ contains all arguments as separate values for iteration. You can also use named parameters through getopts for handling options like -f filename, providing better readability and flexibility for complex scripts.

Sources

  1. Wikipedia - BashCC-BY-SA-4.0
  2. Wikipedia - Shell ScriptCC-BY-SA-4.0

Missing an answer?

Suggest a question and we'll generate an answer for it.