Introduction to Terminal Code
Chances are, If you are using any computer, it’s most likely running Windows or macOS. Or if you like to be a power user like myself, you’re probably using some form of GNU/Linux. Also, you’re probably using an OS with a very user-friendly UI (User Interface). But what if I told you that clicking icons on a desktop wasn’t the only way to interact with your PC? What if you could type keywords to launch scripts and programs?
What if you used…
What is the terminal?
The terminal, or Powershell (for Windows), is the heart of your computer. All computers are just number crunching, command executing pieces of metal at the end of the day. You give it a command. It will execute it. Before fancy and flashy User interfaces and icons, people would only be greeted with a command prompt when they booted up their machine. Just a blinking cursor is waiting for commands from a human. With the command line, you can do whatever you want. If you can do it using a graphical program, you can most likely do it on the command line by hand. Some say that some things are done best via the command line. Many of the things you do on a computer, like text editing, messaging, emailing, and even web browsing could be done on the terminal if you are knowledgeable enough. But be careful because you could very easily break your entire system with a single command if you don’t know what you are doing. Coding and BASH scripting can be done through the terminal, no matter what OS you use. The terminal is a compelling part of your system.
What’s BASH scripting?
BASH (Bourne Again Shell) scripting is a shell command coding language released on June 8th, 3rd, 1989, created by Brian Fox. It was made for the UNIX operating system baked into almost every Linux distribution and the Apple Macintosh operating system. BASH is essentially coding, but with the commands, you would typically use when using the command line in a Linux operating system. It also can only be used for terminal applications. No graphical interfaces. For example, when changing directories in Linux to get to an executable terminal application, you would generally input the following,
[Alejandro123@host ~]$ cd /Desktop[Alejandro123@host ~]$ ls
Downloads Pictures Videos Music Public Cava Firefox Steam
[Alejandro123@host ~]$ cd Cava[Alejandro123@host Cava]$ ls -rMAKEPKGCava.cCava[Alejandro123@host ~]$ ./cava
For this example, I started a terminal program called Cava, a terminal audio visualizer on Linux. It’s an audio visualizer for all audio coming out of the computer. All of those commands I entered could easily be shrunken down to just one command to launch the program using BASH scripting. All you need to have is basic coding knowledge.
How do I make a BASH script?
Coding in BASH is similar to coding in Python or other high-level languages. It’s simple and easy to understand. You can even make if statements and for loops in BASH. But in our case, we will be using simple Linux commands to start the Cava program and create a simple yet effective automation script. To make a BASH file to edit, you have to open any text editing application. You can even code this script within the terminal using Vim or Nano. I will be using Vim in an Arch Linux terminal for this demonstration. For this kind of coding, it’s best to use the terminal, so you can quickly run the script for testing whenever you want to test it.
First, once you open your text editor of choice, it doesn’t matter what you use, as long as you save the file as a .sh file. The first line of code will have to point to the BASH interpreter in your system to execute your code. Without this line, the script will not function because it wouldn’t know what to run correctly. So the first line of code will be:
Next, you list the Linux commands in the correct order. Each command will be a new line in the script Like so:
- cd /Desktop- ls- cd Cava- ls -r- ./Cava
And if you would like confirmation that your script ran successfully, you can add the following:
- echo "Script executed successfully!"
Lastly, to make sure your script does not continue to run and add the following in the end:
Then make sure you save the text file as a .sh file, so Linux recognizes that it’s a script and not a plain text file. You can name the file anything before the .sh extension. If you use the terminal text editor vim, you must declare the script’s name before typing out your code. Also, make sure that you save the file in the correct directory. You can save it anywhere, but the home directory is the best location because your terminal automatically starts you in that directory once it’s opened. Once you’re all done, open up a terminal and type out what you named that script, with .sh at the end. And it should work! The script should open the terminal application cava and visualize all your fancy music!
Why create scripts?
Convenience. Continuously typing out the same commands repeatedly on Linux to open a program is repetitive and time-consuming. Learning BASH and creating scripts will save you more time and effort in the long run when you use the command. It will make you more efficient. Saving time is an essential thing to learn while programming. Typing out redundant code repeatedly wastes time, and in a corporate environment, it wastes money because time is money in the business world. Like any other language you learn, writing code faster and more efficiently is essential. Introducing yourself to the world of BASH scripting and automation will also teach you fundamental programming. It will help you better grasp the concepts, rules, and fundamentals of programming as a whole. Not only does it save you time, but it’s also a great learning experience for aspiring computer scientists/engineers and even the average person just wanting to learn to program.
In conclusion, BASH scripting is a powerful programming tool that you can use to automate tasks and create programs. But remember, with great power comes great responsibility. Although it can be used for good, it can also be used for evil purposes. Hackers often utilize BASH for their gain. Whether it’s deleting all your files, locking you out of your computer, or just for the sake of causing chaos, they can use it for non-ethical purposes. Make sure that if you use scripts that someone else made, look into the source code and double-check before running it on your machine. Also, your BASH scripts could very well break your system, so make sure you know what you’re doing before you run it. Like mentioned earlier, BASH is a very powerful programming tool, and one terrible mistake may cost you your whole computer. Using a script as the superuser (Linux) or Administrator (Windows) is very risky. It allows whatever you’re running to do whatever it wants without asking you for a username or password because you’re signed in as the top user.