How To Execute Sh File In Linux Like A System Administrator

How To Execute Sh File In Linux Like A System Administrator

How to Run Shell Script (.sh) Files in Linux

Executing a shell script (.sh) in Linux requires granting execute permissions using the chmod command followed by invoking the script via a direct path, the bash interpreter, or the source command. Mastering these execution vectors ensures seamless shell automation, precise environment variable scoping, and robust pipeline integration across POSIX-compliant operating systems.


Pre-Operation & Planning Checklist

Running shell scripts in a Linux environment demands a firm grasp of terminal mechanics, file ownership, and access control lists. Before attempting execution, you must verify that your user account possesses the necessary rights to interact with both the target file and the system resources the script attempts to manipulate.



  • Essential Tools & Environment: A POSIX-compliant terminal emulator (Bash, Zsh, Dash), a text editor (Nano, Vim, Emacs), and a user account with standard or elevated (sudo) privileges depending on the script's system requirements.
  • Prerequisite Knowledge & Standards: Familiarity with the Linux hierarchical directory structure, absolute versus relative pathing, standard POSIX permission octals (e.g., 755, 700), and the proper use of the shebang line (#!/bin/bash) at the top of executable text files.
  • Execution Benchmarks: Typical execution preparation takes under 2 minutes, with zero financial cost, relying exclusively on built-in core utilities found in virtually all Linux distributions, including Ubuntu, RHEL, CentOS, Debian, and Fedora.

Step-by-Step Shell Script Execution Workflow



Step 1: Open the Terminal and Navigate to the Script Directory

Launch your preferred terminal interface and use the change directory command, followed by the absolute or relative path of the directory housing your target script. Confirm your current working location by running the print working directory utility, and verify that the target file exists in the directory listing using the list files command with long formatting enabled.

Pro-Tip: Utilize tab-completion by typing the first few letters of the directory or file name and pressing the Tab key to prevent path typos and accelerate navigation.



Step 2: Grant Execution Permissions Using Chmod

By default, newly created text files in Linux lack the executable permission bit to prevent accidental execution of arbitrary code. You must modify the file mode bits using the change mode utility by specifying either symbolic flags (such as adding execute permission for the user owner) or numeric octal modes. For standard operational safety, grant execution rights exclusively to the file owner by applying the appropriate octal setting.

Warning: Never blindly assign universal write and execute permissions to scripts downloaded from untrusted sources, as malicious payloads can compromise entire system directories.



Step 3: Execute the Script via Direct Path or Interpreter

Once the permission bit is active, execute the script using one of several methods depending on whether you want the script to run in a subshell or influence your current shell session. To run the script in an isolated subshell using its relative path, prepend the path specifier to the file name. Alternatively, invoke the bash interpreter explicitly by passing the script file name as an argument to the shell binary, which bypasses the requirement for the file to have its execution permission bit set.


How to run sh file in windows using git bash tutorial - lasopadead

How to run sh file in windows using git bash tutorial - lasopadead

Comparison of Linux Script Execution Methods



Execution Method Command Syntax Subshell Isolation Requires Execute Bit Best Used For
Relative Path ./script.sh Yes Yes Standard script execution within the working directory
Absolute Path /home/user/script.sh Yes Yes Automated cron jobs, scripts called by absolute references
Explicit Interpreter bash script.sh Yes No Testing scripts, running files lacking permission bits
Source Built-in source script.sh or . script.sh No (Current Shell) No Exporting environment variables and shell functions

Common Site Failures & Field Fixes



  • Permission Denied Error:



    • Root Cause: The script file lacks the executable permission bit, or the user attempting execution does not own the file and lacks read permissions.
    • Actionable Fix: Apply execution permissions to the file using the change mode utility, or run the script by prefixing the command with the bash interpreter explicitly.
  • No Such File or Directory Error:



    • Root Cause: Typographical errors in the file path, running a relative path command without the leading path specifier, or attempting to execute a script that resides outside the current working directory.
    • Actionable Fix: Verify the exact file location using the list utility, use tab-completion for path entry, or provide the complete absolute path to the target file.
  • Bad Interpreter: No Such File or Directory Error:



    • Root Cause: The shebang line at the very top of the script points to an interpreter path (such as /bin/bash) that does not exist on your specific system, or the file contains Windows-style carriage return characters (CRLF).
    • Actionable Fix: Inspect the first line of the script to ensure the interpreter path is correct, and convert Windows line endings to Unix line endings using the command-line conversion utility.

Frequently Asked Questions



Do I always need to use chmod to run a sh file?

No, you do not need to change file permissions if you invoke the script by explicitly calling the shell interpreter beforehand. Running the bash command followed by the script file name bypasses the executable permission requirement, whereas running the script via its direct path requires the execution bit to be set.



What is the difference between running ./script.sh and source script.sh?

Running a script via a direct path spawns a separate child process (subshell) to execute the commands, meaning any exported variables or directory changes do not affect your main terminal session. Using the source command executes the script line-by-line within the current shell environment, making it ideal for updating environment variables and loading shell profiles.



Why do I get a permission denied error even after running chmod?

This error typically occurs if the directory containing the script lacks read or execute permissions for your user account, or if the underlying filesystem is mounted with mount flags that explicitly block execution, such as the noexec flag commonly applied to external drives or /tmp partitions. Check the mount options and parent directory permissions to resolve the issue.



How do I run a shell script with root privileges?

You can execute a shell script with administrative privileges by prepending the superuser do command to your execution syntax. Ensure you trust the script entirely before elevating privileges, as scripts running under the root user can modify critical system files and binaries.

Execute your Linux shell scripts with absolute confidence by applying proper permission controls and choosing the optimal execution vector for your workflow today.


How to Execute .sh Files. Overview | by Andrew Dass | Medium

How to Execute .sh Files. Overview | by Andrew Dass | Medium

Read also: Viewers react to the sudden cancellation of a nashville television show