One of the most powerful features in Linux is the ability to create your own executable files. In Windows these are EXE, BAT or MSI. In Linux, ANYTHING can be made executable and today I will show you how to make a simple shell script, make it executable, then you can run it.
First we need to define an interpreter for the script. Since we are covering bash scripts in this section we will go with the usual shebang "#!/bin/bash" anything in your script which has been commented out will be ignored... these are lines starting with a #. Only the first line isn't ignored in that it defined the interpreter.
#!/bin/bashUsing ANY text editor we type the preceding information into a new document and we save the file as "firstscript.sh". We could name it anything we want but the ".sh" lets us know we can run it as a "shell" script. Now we need to make it executable...
#This is my first script
echo "Hello world"
chmod -x firstscript.shOnce the script is executable, you can run it with
sh firstscript.shand the output would be...
Hello worldJust about any commands you can enter into your shell can be made into a bash script. There are lots of reasons why you would want to make your own bash scripts and we will explore some of those reasons in this section of the forum.