Automate your life with BASH
Bash, or Bourne Again Shell, is a command language tool that allows users to interact with their operating systems to perform any task. Bash is a command-line interpreter, commonly referred to as an operating system shell, that gives users the ability to control their systems without having to mess with the low level kernel interface. Bash, or a variation of it, exists natively on most operating systems, or can be installed on Windows based machines. Unlike most programs we use, bash is text based. This means that there is no graphical interface that you can interact with. Instead, we can input what are called commands. While this might seem limiting at first, if you imagine it more like being able to talk to your computer the possibilities open up immensely. If you’ve ever used a linux or mac terminal, you’ve probably already performed bash commands!
What’s the point?
Why should you care about being able to control your computer with text rather than using your mouse on visual interfaces? The beauty of bash is that you can create scripts which contain a set of commands that you can then execute whenever you want. Imagine it like a list of chores that you might have been given when you were younger. Scripts are lists of chores that you want your computer to do without you having to manually tell it every time you want something done. This is where the fun begins!
What can we do with bash?
Bash can perform simple tasks like file and folder operations to stuff like editing documents, converting files, and even downloading programs from a site and installing it. You can also do more complex tasks like running a web server to setting up automatic file backups. Bash gives you the ability to talk to different parts of your system and perform a wide variety of objectives. In this article, we’ll learn how to perform some common bash commands and we will create a simple bash script to kick start your journey into this new world of possibilities!
Let’s start making commands!
To begin, open up a terminal window. If you’re on windows, you’ll need to search up how to get bash running on your system. Now, type in the command:
You should get an output that looks like this:
This tells you what version of bash your system is running. If you’re not getting an output or you’re getting an error message this means your terminal is not running bash. Otherwise, congratulations on making a bash command! In this line we’re utilizing the echo command that tells bash to output the resulting text into the terminal so that we can read it. The $ in front of BASH_VERSION tells bash that this is a variable name and to substitute it with the contents of that variable. Now, let’s have some more fun!
Common commands you’ve probably used before include cd, ls, rm, and mv. These are the commands that let you change directory, list the files in your current directory, remove a file, and move a file respectively. If you want to quickly see a bunch of different commands, enter help. This gives you a lot of information about what you can do and how to use them. If you want to learn more about a particular command, enter man followed by the command name. The man command returns a manual. Now that you’ve gotten more familiar with bash and some common commands, let’s move on to writing a script!
To begin writing a script, either open your favorite text editor or type in nano in your terminal to open up a simple editor. Now in the first line type in:
This tells your system that the following commands are to be used with bash. It’s not always needed but it’s better to have it there to prevent any errors. Now, we’re going to utilize the read command which waits for the user to enter in text that you can then process later on. So on a new line, type in:
In this line, we’re using the read command with parameter -p to prompt the following message on the terminal. The last argument name is what we want to call the variable for the user’s input as. Finally, on the last line, enter:
We’re using the echo command to display the following text, and telling the system to replace $name with the information stored in the name variable. Now save the file as hello.sh. If you’re using nano, use ctrl + x to then save the file. The .sh file extension is what is typically used for a shell script. You can also save it as a .txt if you like. Before we actually run the script, we need to mark the file such that the system knows it can be run as an executable. To do this, go to your terminal window and make sure that you are in the same directory as the file you just made. Now enter in the command:
The chmod command stands for change mode, and allows you to change the properties of a file including what users can access it, modify it, or execute it. The 700 tells chmod to set the file such that only you the user can read, write, or execute the file. Now that this is done, it’s time to test out your new script! Enter the following into your terminal to receive a friendly greeting:
Now that you’ve learned the basics of bash, it’s time for you to get out there and explore the endless possibilities! For a huge list of premade scripts and resources, check out this github repository. Hopefully you learned something new and are already thinking of new ways to interact with your computer. Thanks for reading!