How To Execute SH File In Linux: A Comprehensive Guide To Running Shell Scripts

How To Execute SH File In Linux: A Comprehensive Guide To Running Shell Scripts

How to Execute INSTALL.sh Files in Linux Using Terminal

Executing a shell script in Linux requires navigating file permissions through the terminal and assigning executable rights using the chmod command before running the script with an interpreter or direct path execution. Mastery of these operational workflows ensures system administrators and developers can automate complex tasks securely and efficiently across modern Linux distributions.


Pre-Operation & Initial Requirements

Before executing shell scripts on a Linux environment, understanding the underlying permissions framework and file structures is essential for maintaining system security and operational integrity. Shell scripts are plain text files containing a sequence of commands executed by a command-line interpreter such as Bash, Zsh, or Dash, and they demand specific system states before they can run successfully.



  • Essential tools and environments: A POSIX-compliant Linux shell environment (Bash version 4.0 or newer is standard), a terminal emulator such as GNOME Terminal, Konsole, or Alacritty, and a text editor like Nano, Vim, orgedit for script verification.
  • Mandatory prerequisite knowledge: Familiarity with basic Linux directory navigation, the hierarchical file system standard, and the conceptual differences between the root user and standard user privileges.
  • Estimated duration and benchmarks: Completing the permission adjustment and execution process takes under two minutes, assuming the target script is already authored and syntactically correct.

Step-by-Step Shell Script Execution Workflow



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

Launch your command-line interface and use the change directory command, abbreviated as cd, followed by the absolute or relative path to the folder where your target shell script resides. Confirm your current working directory by executing the print working directory command, pwd, and list the directory contents using the list command, ls -l, to inspect the target file attributes and current permission bits.

Pro-Tip: Use the Tab key for auto-completion when typing long directory paths or script names to prevent typographical errors and speed up navigation.



Step 2: Grant Execution Permissions to the Script File

By default, newly created text files in Linux do not possess execution rights for security reasons, displaying permissions like read and write only. To change this status, invoke the change mode command, chmod, paired with the user-executable flag by typing chmod +x filename.sh, replacing filename.sh with your actual script title. This modifies the file inode metadata to permit execution by the owner, group, or others depending on your specific arguments.

Warning: Never assign overly permissive rights such as 777 to production scripts unless explicitly required, as this allows any system user to modify and execute arbitrary commands with your privilege level.



Step 3: Execute the Shell Script Using Your Preferred Method

Run the script using one of several standard invocation methods depending on your isolation and environment needs. For direct execution from the current directory, prefix the file name with a dot and slash, as in ./filename.sh, which executes the script in a subshell using the interpreter specified in the shebang line. Alternatively, invoke the interpreter explicitly by typing bash filename.sh, or execute the script with administrative superuser privileges by prepending the command with sudo if the underlying tasks modify system configurations.


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

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

Linux Script Execution Methods and Technical Specifications



Execution Method Command Syntax Subshell Behavior Environment Variable Inheritance Best Use Case
Direct Execution ./script.sh Runs in a dedicated subshell Inherits exported variables only Standard script execution with a shebang
Explicit Bash Call bash script.sh Runs in a new Bash subshell Inherits exported variables only Running scripts missing an executable bit or shebang
Source Built-in source script.sh or . script.sh Executes in the current shell process Modifies current shell environment directly Loading configuration files and environment exports
Superuser Invocation sudo ./script.sh Runs in a privileged root subshell Resets or restricts certain environment variables System-wide administrative tasks and service management

Common Script Execution Failures and Field Fixes



  • Permission Denied Error: The script fails to run, returning a permission denied message in the terminal output.

    • Root Cause: The file lacks the execution bit required by the operating system kernel to run binaries or scripts directly.
    • Actionable Fix: Run chmod +x filename.sh to grant execute permissions, then retry the execution command.
  • Carriage Return / Bad Interpreter Error: The system returns an error stating that bin/bash\r: no such file or directory exists.

    • Root Cause: The script was edited and saved on a Windows operating system, introducing DOS-style carriage return characters (CRLF) that confuse the Linux interpreter.
    • Actionable Fix: Convert the file line endings to Unix format using the dos2unix filename.sh utility or by opening the file in a Linux text editor and saving it with LF endings.
  • Command Not Found Within Script: The shell executes the script wrapper, but individual internal commands fail with command not found errors.

    • Root Cause: The environment PATH variable for the running subshell does not include the directory where the required binaries reside, or dependencies are missing.
    • Actionable Fix: Use absolute paths for internal binaries within your script or ensure the necessary software packages are installed on the target distribution.

Frequently Asked Questions



What is a shebang and why is it required in a sh file?

The shebang is the character sequence consisting of an exclamation point and number sign at the very beginning of the script file, followed by the path to the interpreter, such as bin/bash. It instructs the operating system kernel which program interpreter to load automatically when the script is executed directly via path invocation.



Can I run a shell script without giving it execution permissions?

Yes, you can bypass changing file permissions by explicitly invoking the script interpreter from the command line, such as typing bash filename.sh or sh filename.sh. This method passes the script file as an argument to the interpreter directly, eliminating the need for the execute permission bit on the file itself.



What is the difference between running a script with sh and bash?

Invoking a script with sh on many modern Linux distributions invokes a symbolic link to a strict POSIX-compliant shell like dash, which lacks advanced Bash-specific syntax features. Running the script with bash ensures full compatibility with extended array handling, string manipulation, and bash-specific built-in commands.



How do I run a shell script in the background so I can close the terminal?

You can execute the script in the background by appending an ampersand to the command, such as ./script.sh &, and use the nohup command prefix to prevent the script from terminating if the terminal session closes. Combining these tools as nohup ./script.sh > output.log 2>&1 & ensures continuous execution and log retention.

Master Linux Automation and Scripting Best Practices Today

Enhance your system administration workflow by exploring advanced shell scripting methodologies and mastering robust automation techniques for enterprise Linux environments. Implement secure permission protocols and robust error handling to build resilient operational pipelines across all your deployments.


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

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

Read also: How to Reheat Pork Ribs Without Drying Them Out