How To Make Bash Script Executable? [3 Proven Methods]

Written by

Reviewed by

Last updated: June 16, 2024

Expert verified

SVG Image

TL;DR

    To make Bash Script executable, follow these steps:

    1. In a text editor, type your script starting with #!/bin/bash for bash execution, followed by your commands, and save as filename.sh.
    2. Use chmod +x filename.sh in the terminal to grant execution permissions to your script.
    3. Execute your script by typing ./filename.sh in the terminal.
    4. Witness your script running successfully and displaying its output on the screen.

    Ever written a bash script and wondered why it won’t run? The key is making it executable. This post is your quick guide to doing just that. I’ll use a simple command called chmod to change your script’s permissions, allowing your computer to run it. It’s an easy process, and I’ll walk you through each step. By the end, you’ll have a script that’s not just text but a command your computer can execute. Plus, I’ll explain how to keep your script safe and solve any common problems you might face. Let’s explore and get your script up and running!

    What is a Bash Script?

    A Bash script is a text file that contains a series of commands for the Bash shell to execute. Bash, which stands for “Bourne Again Shell,” is a command-line interpreter that comes with most Unix-like operating systems, including Linux. These scripts can automate tasks, run programs, and perform complex operations without manual input.

    Key Points:

    • Text File: A Bash script is a plain text file.
    • Commands: It contains commands that you would normally type in the terminal.
    • Automation: Scripts help automate repetitive tasks, making your work more efficient.

    Here’s a simple Bash script called hello.sh:

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

    When you run this script, it prints “Hello, World!” to the terminal.

    Significance of Making a Bash Script Executable

    Making a Bash script executable means you can run it directly without typing bash or sh before the script name. This is important because it simplifies the process of running scripts and ensures they behave like regular programs.

    Why It’s Important:

    • Convenience: You can run the script with a single command.
    • Efficiency: It saves time, especially if you run the script frequently.
    • Consistency: It ensures the script runs with the intended interpreter.

    How to Make Bash Script Executable?

    To make Bash script executable with chmod, you need to grant the execute permission to the file. You can do this by running the command chmod +x scriptname.sh, where scriptname.sh is the name of your Bash script. This command adds the execute permission for the user, group, and others, allowing anyone who can access the script to run it. If you want to be more restrictive with permissions, you can specify more precisely, such as chmod u+x scriptname.sh to grant execute permission only to the file’s owner.

    Here is the step-by-step guide for three different methods to make bash script executable:

    1. Using chmod Command

    The chmod command is used to change the file permissions, making your Bash script executable. This is the most common and straightforward method for enabling script execution. Here is how to use chmod for bash script:

    1. Open a Text Editor. You can use Notepad on Windows and TextEdit on Mac.
    opening text editor
    1. Now, write a script. Here’s a simple script to get you started. Copy this into your editor:
    #!/bin/bash
    echo "Hello, wonderful world of scripting!"

    This line #!/bin/bash tells your computer that this script should be run in the bash shell, and the echo command prints out the message in quotes.

    creating script file
    1. Save your file with something descriptive and a .sh extension, for example, greet_the_world.sh. The .sh helps identify the file as a shell script.
    saving script file
    1. Now open your Terminal application.
    open your terminal application
    1. Navigate to the directory where you saved the script using the cd command. If you saved it on your desktop, use the command: 
    cd Desktop
    navigating to the directory
    1. Before making changes, it’s wise to see the current state. This will help you confirm your actions have an effect. Type the following command: 
    ls -l greet_the_world.sh

    Look for letters at the start of the output line, if there’s no x, it’s not executable yet.

    checking current state of file
    1. Now run the following command and hit Enter:
    chmod +x greet_the_world.sh

    This command changes the mode of the file to add (+) execution rights (x).

    making the file executable
    1. Run this command again:
    ls -l greet_the_world.sh

    You should see an x in your permissions, indicating success.

    verifying the execution rights
    1. With your script now executable, it’s showtime. Type:
    ./greet_the_world.sh

    This command will execute the script.

    executing script file
    1. If all has gone well, you’ll see “Hello, wonderful world of scripting!” printed back to you. 

    2. Using chmod with Numeric Mode

    Using numeric mode with chmod is a quicker way to set permissions. This method is preferred by experienced users for its simplicity and precision. Here is how to make a bash script executable:

    1. Use the ls -l command to check the current permissions of your script.
    ls -l script.sh
    checking current permission of the script
    1. Use the chmod command with numeric values. 755 is a common setting that allows the owner to read, write, and execute, and others to read and execute.
    chmod 755 script.sh
    chaning permissions of file using numeric values
    1. Check the updated permissions to ensure the script is now executable.
    ls -l script.sh
    rechecking file permissions to verify the changes

    3. Using GUI File Manager

    For users who prefer a graphical interface, you can change file permissions using your file manager. This method is user-friendly and doesn’t require command-line knowledge. Here is the step-by-step method to make bash script executable Linux:

    1. Open your preferred file manager (e.g., Nautilus, Dolphin).
    opening file manager from application menu
    1. Right-click the script file and select Properties.
    opening script file properties
    1. Go to the Permissions tab.
    go to permissions tab
    1. Check the Allow executing file as program option.
    changing file permissions using gui

    8 Best Practices for Script Permissions

    Managing the permissions of your bash scripts is crucial for maintaining a secure and efficient computing environment. Here are eight best practices for script permissions, ensuring your scripts are accessible only to those who truly need them and are protected from unauthorized use or modification.

    • 🛡️ Keep scripts as private as possible: Limit access to your scripts strictly to users who need it for their tasks. Unnecessary public access can lead to security breaches and unintended script execution.
    • 🔑 Use the least privilege principle: Assign the minimum permissions necessary for your scripts to function. This approach minimizes potential damage from security vulnerabilities by restricting access and capabilities.
    • 🔍 Regularly review permissions: Periodically check script permissions to ensure only authorized users have access. This preventive measure helps avoid security risks by identifying and rectifying inappropriate access permissions.
    • 🌐 Secure your scripts in shared environments: In environments with multiple users, ensure your scripts cannot be modified or executed by unauthorized individuals. This protects against malicious alterations and executions.
    • Be careful with executable permissions: Before granting executable permissions to a script, evaluate its necessity. Unwarranted executable permissions can expose systems to unnecessary risk.
    • 🔄 Update permissions when roles change: Adjust script permissions in response to team changes, such as departures or role modifications. Keeping permissions aligned with current roles prevents unauthorized access.
    • 🚨 Keep an eye on automated scripts: Monitor and regularly review the permissions of scripts that run automatically. These scripts often operate with elevated privileges, posing a significant security risk if compromised.
    • 🔐 Encrypt sensitive scripts: For scripts that handle sensitive information or perform critical functions, consider encryption to safeguard against unauthorized access and enhance security. Encryption acts as an additional protective layer for sensitive scripts.

    Bash Make Script Executable: Wrapping it Up

    In conclusion, making a Bash script executable is crucial for running your scripts smoothly. I’ve looked at methods like using the chmod command and the GUI file manager.

    If you want to explore more, check out these articles

    Frequently Asked Questions

    What is chmod Command?

    chmod is a command in Linux and Unix-like operating systems used to change the file access permissions. The name chmod stands for “change mode,” and it allows users to set or modify the read, write, and execute permissions for files and directories. The permissions control the actions that can be performed on the files by different users. The command can be used with symbolic representations like r, w, and x, or with numeric octal values such as 644 or 755 to set specific permissions.

    How do I make a script executable for all users but only editable by me?

    To make your script executable by everyone but keep it editable only by you, use the chmod command with specific permissions. First, ensure you’re the owner and then apply chmod 755 yourscript.sh. This setting allows all users to execute the script while restricting editing rights to the owner only.

    Can I undo chmod changes to a script?

    Yes, you can revert chmod changes by applying a different permission set. If you remember the original permissions, use chmod followed by the permission numbers and the script name, such as chmod 644 yourscript.sh, to restore its previous state. Always note down original permissions if you anticipate needing to revert.

    What is the significance of the #!/bin/bash shebang in scripts?

    The #!/bin/bash shebang at the start of a script specifies that the script should be executed using the Bash shell. It’s essential for ensuring that the correct interpreter is used, regardless of the environment’s default shell, making your script more portable and reliable across different systems.

    What’s the best practice for naming Bash scripts, and does it affect execution?

    While naming conventions for Bash scripts don’t directly impact execution, sticking to clear, descriptive names with a .sh extension is best practice. This helps with the easy identification of script files and their purpose. Avoid spaces and special characters in names to simplify execution and reduce the need for quoting filenames.

    Ojash

    Author

    Ojash is a skilled Linux expert and tech writer with over a decade of experience. He has extensive knowledge of Linux's file system, command-line interface, and software installations. Ojash is also an expert in shell scripting and automation, with experience in Bash, Python, and Perl. He has published numerous articles on Linux in various online publications, making him a valuable resource for both seasoned Linux users and beginners. Ojash is also an active member of the Linux community and participates in Linux forums.

    Akshat

    Reviewer

    Akshat is a software engineer, product designer and the co-founder of Scrutify. He's an experienced Linux professional and the senior editor of this blog. He is also an open-source contributor to many projects on Github and has written several technical guides on Linux. Apart from that, he’s also actively sharing his ideas and tutorials on Medium and Attirer. As the editor of this blog, Akshat brings his wealth of knowledge and experience to provide readers with valuable insights and advice on a wide range of Linux-related topics.

    Share this article
    Shareable URL
    Prev Post

    6 Best Methods to Use the Read Command in Linux

    Next Post

    4 Effective Methods to Recursively List All Files in a Directory Linux

    Read next