Executable Script
Once your script is saved, you will need to make it executable by running the command quotchmod x scriptnamequot in the terminal. To run the script, you can use the terminal command quot.scriptnamequot. If your script is located in a directory that is not in your system's PATH, you will need to specify the full path to the script when running it.
Here's how I encapsulated it into a script that saves me keystrokes at the command line. 1 Create a new text file with a .sh extension. I created a new file called deploy.sh for my website. 2 Add !binbash to the top of it. This is necessary for the quotmake it executablequot part. 3 Add lines that you'd normally type at the command line.
To make a file executable in Linux, the executable mode bit needs to be enabled. To set the executable mode bit, the chmod command is used like this chmod ux ltfilegt With that, you can execute said file from the terminal .file. That was the quick summary. Let's see things a bit in detail. Make a file executable in Linux
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.
Copy this script into a file e.g., make_executable.sh. Run it in your terminal by typing bash make_executable.sh. This will Create a hello file with the Bash script. Display the contents of the file. Show the file's initial permissions. Change the file's permissions to make it executable. Verify the new permissions.
A script doesn't have to end with .sh it can end in .txt as shown here, or have no extension whatsoever. Instead of just x only you can execute the script, use ax so all users can execute the script. The file name has a appended to it to indicate it is executable. Also the file name changes color to green on most systems.
Here, I used the same Bash script file quottest.shquot which I tried to run at the beginning of this article, but this time I ran it after adding executable permission.As a result, the script is successfully executed amp is displaying the output quotHello!quot.. 2. Make Bash File Executable Using GUI
The next step in making a bash script executable involves changing its file permissions. This is achieved through the chmod command. What is chmod? chmod, short for quotchange mode,quot is a UnixLinux command used to alter the access permissions of files and directories. Making a Bash Script Executable
You first need to start your script with '!binbash ' and save it as lt filename gt.sh Also make sure that you keep the permissions as ax i.e all users can execute the script.
The second way to execute a bash script is by setting up the executable permissions. To make a script executable for the owner of the file, use ux filename. chmod ux hello_script.sh. To make the file executable for all users use x filename or ax filename. chmod x hello_script.sh Step 5 Running Executable Script. After you have assigned