Like Windows, where only the .exe files are executable, you can execute any file in your Linux system. Now the question is, why do you want to make your file executable? Suppose you have some commands that you run regularly. You can add all those commands into a file and then execute that file. So, instead of running the same command again and again, you will have to execute that file to run the commands whenever you want.
Sounds interesting and time-saving right? Let’s dive into the article to find out how to make your file executable and ease your workload.
Also read: If-else or Switch case: When to use which one?
How to create an executable file in Linux?
Step 1: Create a file using the cat command with the following text and press CNTRL+Z to quit and save your files.
# cat > Welcome
#!/bin/bash
echo "Hello everyone!"
echo "Welcome to Candid.Technology."
The ‘#!/bin/bash’ command tells your shell to run all of the commands written after it in the script.
Now, if you view the file’s content, you will see the echo command that you wrote in your file.
Step 2: To make your file executable, you need to give the execute right to the owner using the chmod command.
# chmod u+x Welcome
The chmod command written above will assign the execute right to the owner of the file.
Execute the ls -l command, you can see that the owner of your file now has the execute command ‘x’, and the colour of your filename has changed.
Step 3: Your file is now executable. Run the following command in your terminal to see the output.
# ./Welcome
Similarly, to make your file executable, add your commands into the file after the ‘#!/bin/bash’ command, give the execute right to the user of your file and then run it using the ‘./<name of your file>’ command.
Also read: How to solve Arduino error: ‘was not declared in this scope’?