The Basics of Bash
Bash is a scripting language that is run inside a terminal on an Operating System. It is most commonly used for creating and manipulating files and folders on the hard drive. However, it has much more capability than just that. Becoming familiar with Bash is crucial for any programmer, so in this article, I'm going to cover the very basics of the scripting language.
Navigation
The first thing you'll need to know to use Bash is basic navigation. When you open your terminal, it's just going to be blank, so how do we list the current directory? That's simple, just use the command pwd
. Now that will show you your current directory, likely at the home folder, something like /home/username
on Linux. Now, we know the directory, how do we list the files and folders contained within the directory? Simply use the command ls
. That will list all the visible files and folders in your current directory. I say visible because some files and folders can become hidden if the name of the file or folder starts with a .
, for example, .bashrc
. To view hidden files and folders, you need to add what's called a flag to the ls command, by entering ls -a
. There are other flags that can be used with the ls command, but I will not be going over those here. Now that we can list all the contents of our directory, how do we enter a new directory? By using the command cd <directory>
. For example, say you want to view the Desktop folder, and you're currently in the home folder, you would type cd Desktop
. Now that we know how to list and navigate the files and folders, we need to know how to make them.
Creating files and folders
Creating a folder in Bash is very easy, all you need to do is use the command mkdir <folder name>
. For example, say you want to create a new directory for all your Projects, from the home folder, you could type mkdir Projects
and the folder would be created. Now, say you enter the Projects directory by typing cd Projects
, how would you create a new Python file? Just type the command touch <file name>
. For example, you would enter the directory and type touch web_scraper.py
. Now that you created the Python file, you're going to want to run it.
Running scripts using Bash
Now, you could just run the Python file using the command python <file name>
, and that would work. There is a more universal way to run any Script from your Bash terminal. If you want to run it like you would any other script on your system, you need to do a few things. Firstly, you will need to add the following line to the top of your file #!/usr/bin/python3
. This is going to tell the system that this file should be run using Python. Now, you need to ensure your script is an executable file. You can do this by typing the command chmod +x <file name>
. This will turn your Python script into an executable. Now, you can run the script by using the command ./<file name>
. The ./
just means that you are accessing a file in your current directory (i.e. /home/username/projects).
Conclusion
While there is still much more to Bash, including writing your own scripts using Bash (instead of using Python, if you wish), this brief tutorial just gave you the basics you need to know to start using it. Now that you know how to list, navigate, and create files and folders, as well as how to execute scripts, you're well on your way to mastering Bash.