Bash


Uncategorized

Updated Jun 8th, 2022

Overview

What is Bash? A legitimate interface to your computer, and it’s not just for server admins and programmers. It can be your desktop, your word processor, your graphics.

What is Bash Take 2: Unix shell and command language written by Brian Fox for the GNU Project as a free software replacement for the Bourne shell. First released in 1989, it has been used as the default login shell for most Linux distributions. A version is also available for Windows 10 via the Windows Subsystem for Linux.

What is Bash Take 3: Bash is also a programming language that allows us to write scripts, (automate commands).

Bash stands for “Born Again Shell” because the most popular Bourne shell created in 1979 was overtaken by BASH in 1989.

“It is called the Bash Shell because it kind of wraps itself around the Linux operating system shielding us from all the scary inner workings of Linux because honestly, we probably couldn’t handle it.” – Network Chuck

Note: In most Linux systems, the default shell is bash but we can change that to any other shell-like program like “zsh”, ‘fish,” “sh,” etc. Confirm with the “which $SHELL” command.

Note: After installing lubuntu on a old computer of mine the default shell was in fact bash 5.0.17

Note: If you have Git Bash installed on a windows machine, no surprise you also have access to bash.

From the article listed below: “Someone once told me if you did it (or plan to do it) more than 5 times, automate it! I took this very seriously and the more comfortable I felt using VIM and bash scripts, the more things I was automating.”

Get Hands Dirty

bash files end in “.sh” so create a new file with “touch beerme.sh” or no file extension at all.

The first line in a bash file should be a shebang “#!” followed by the path to the application that should run it, “#!/usr/bin/bash.”

#!/usr/bin/bash

echo "my first bash script"

GREET="Howdy Partner"
echo $GREET

File is interpreted line by line.

Execute the script by typing the file name into the shell

./beerme.sh

If you get permission denied when trying to run a script, it is missing the executable permission to run. Check this out by using “ls -l” to check file permissions in Linux. See something like: “–rw-r–r–” which is what you can and cannot do with a file, (Read “r” Write “w” and Execute “x”). Use the “chmod” command to update permissions, (chmod +x ./nameofscript.sh).

Can pass in positional arguments by adding right after the name of your script when running the file (leave a space).

Can also get user input and use while loops.

Conditional logic is a thing.

Can run processes in parallel in the background with an ampersand.

Sleep between lines of code, “sleep 1”

Sources

Fireship here

NetworkChuck here and here

Also came across a good article here about automating anything you’ve done 5 times with Bash.