In this post I wanted to go over some basic Bash Scripting. Once you know the basics of bash for navigating file systems and executing commands learning how to write basic scripts can be helpful as well. For instance with bash scripting you can regularly move files from one system to another, rename a large number of files and folders, or perform a certain set of operations every time a system boots up. I’ll cover Variables, If statements, and Loops.
Using your Bash terminal find a nice directory to work in and enter the command
This will create a file with the conventional
.sh file extension. Open this file up in your favorite editor and add
#!/bin/bash on the very first line. This is called a shebang and it describes the interpreter that we’re going to use (in this case bash). Keep in mind that octothorps(#) can be used to write comments in your code.
Lets add a simple command to test this out. I’m gonna try this
echo Greetings Planet.
Now save your file, go back to the command line and type
./myBashScript.sh. You will probably get a permissions related error. By default this file is not executable for security reasons. You can change this by using the
chmod command which can modify permissions. Try this
chmod 755 myBashScript.sh. Now you should be able to run
./myBashScript.sh and get the expected results.