What is Bash
In this introduction to Bash, we’re gonna explain what Bash is, how to use it and what it can do for Automation.
Bash stands for “Bourne Again Shell” and was developed by Brian Fox in 1989 and it was written in C. It’s called “Bourne Again” because it replaces the popular Bourne Shell which is “born again” through Bash.
Starting commands
We’ll mention some simple commands for beginners. All of them are non destructive so you can experiment with them as much as you like.
ls – Lists file and folder structure in current directory
pwd – prints current location (path)
man – manual ( man *command name*)
hostnamectl – prints host’s name and information
cat – prints the contents of a file
clear – clears the screen
cd – change directory
mkdir – Creates a directory with given name
Grep command
Grep is one very useful command and I thought it should get a little bit of explaining unlike the commands listed above. It filters your results by rows that matches the keyword. See the picture bellow to get a better understanding of it.
So, you can see, the more detailed we get, the less rows we end up getting. You can always grep “a”, but that will return every service that contains the letter “a” in it so it isn’t very useful. Grep uses the pipe that will be explained later.
The problem with Bash
The problem with Bash is that Bash isn’t the same on every Linux distribution and some commands don’t work with some Linux distributions and versions.
Not only that, Bash returns text, unlike PowerShell that returns .NET objects. So, you can’t manipulate the output as easy as you would with PowerShell. Sometimes that’s not so important, and sometimes it can turn into a nightmare.
Example
If we run this simple script.
If you’ve read our introduction to PowerShell, you might notice that this command does something similar to the script we ran in that article.
All good for now, but some Linux distributions might not have awk command and you won’t have the right to install it or it might not even be supported. Also, we picked columns 1 and 4, but some services might have an additional column somewhere in between. The problem is that you can’t just add services to your script and get the data for all of them if they all do not have the same output format. Things might even break later with updates.
In PowerShell you don’t have that problem because you just pick the objects you want to include in your result and that’s all. The’re always the same, no matter what other columns are different.
I’ve, personally, had a problem in Bash where only one service had the dot as the first column. This symbol: ” ● “. So I had to work around that, and that symbol can be removed with sed command, but there is no guarantee that you’ll have that command at your disposal.
Don’t get discouraged, most commands are available on every Linux, but you might have a different situation one day.
How to use Bash
Since this is an introduction to bash, we will show you how to use it. You usually start Bash on a Linux system selecting the Terminal icon, or if you connect to a system through SSH connection, you’re already inside Bash.
Help
Just like there’s Get-Help command in PowerShell, there’s a man command in Bash. They work pretty much the same and are used in the same way.
We used man with cat to display the manual for cat command. Cat command is simply used to see the contents of a file, but it can do much more. With cat, you can preview multiple files, append contents of one file to another, create a file, reverse it to tac to see the file’s contents in reverse, etc.
Tab
Bash has an autocomplete feature with tab button, but it isn’t always present and doesn’t autocomplete unless you have only one option to autocomplete. When there are multiple options, tab will display those options.
You can see when I type cd 1, it shows me all directories that start with 1, but when I type cd 13, it autocompleted the directory name because it’s the only one with name that starts with “13”.
Also, when I type c it asks me if I wanna se all 308 possibilities, so it doesn’t let me list through commands that start with “c”. It can autocomplete commands, but it also show global files.
When I try tab with cd, it shows me cd command, but also global files that start with “cd”.
The pipe
Pipe is a very powerful tool that enables your to use the results from your previous command for another command. Usually it’s used to store, parse or to convert something. Basically, if you want to use step results, you need to use pipe. The pipe sign is ” | “.
Because we already showed how the pipe works in our previous gif, you can preview it again.
Testing and script development
We won’t get into much detail here since we’ll probably have another article regarding this topic.
Bash scripts have an .sh extension. Here’s a quick guide on how to create and run .sh scripts:
- Go to the directory where you want to create your script: touch <fileName.sh> or cat > <fileName.sh> (there are other ways also).
- Create a file with .sh extension.
- Write the script in the file using an editor of you choice.
- Make the script executable: chmod +x <fileName>.
- Run the script using ./<fileName>.
Bash on different Linux distributions
We recommned that you try your scripts on different Linux distributions. You can do that easily by running Virtual Machines with different Linux distributions on your computer. Some options for that are Virtual Box and VMware.
Bash on Windows
You can run some Bash commands on Windows by running Git Bash.
You can also enable Windows subsystem for Linux. It’s a new feature enabled by partnership between Canolical (parent company of Ubuntu) and Microsoft. You can enable it and start using it on newer Windows 10 version, specifically Windows 10 version 2004, build 19041.450. Microsoft has a full documentation page on how to enable it.
Bash in automation
To automate stuff on Linux, you have to use Bash shell, just like you’d use PowerShell on Windows. Some things are really easy to do in Bash, while some are not. For example, updating is a piece of cake in Bash, while you cannot do it through PowerShell at all. Well, not directly anyway.
Here’s a little script written and executed in bash, similar to the script we ran in out introduction to PowerShell article.
As with that one, this one can also be automated to run every hour or something like that. So you’ll have an hourly service monitoring. Of course the script has to be optimized a bit and standardized in order to be usable.