Learning bash is extremely useful when it comes to hacking, becoming a cloud engineer, gathering information on systems in IT and much more. Learning bash will give you the power to automate tasks and thus make you faster and more efficient. The true computer Wizards of the world are consistently expanding their skills and learning new things, and bash is something you should take some time to get familiar with. We will be following NetworkChuck in this tutorial.
What is BASH:
BASH stands for Bourne Again Shell. It is name this because the shell of Linux was recreated to include some much needed improvements. This is called the bash shell because it wraps itself around the Linux Kernel to help simplify Linux. This makes functions such as adding users and creating or moving files easier for the end user.
Creating A Linux Enviornment:
Most people are running on a pc that uses Windows and making the jump directly over to Linux is intimidating. My solution to have a Windows pc for gaming and a Linux machine for learning was to create a virtual environment. To do this I simply installed Oracle VM and grabbed an ISO of a Linux distribution I wanted to learn and added that ISO file as a bootable media in the VM. I have recently started to do some network pen testing so I chose to go with Kali, which has many built in tools for hacking or pen testing but you could go with any Debian/Arch/Ubunu/Fedora distro and many more. All of these Linux OS's will have a terminal that will allow you to write BASH.
Executing our First Bash Commands:
If you run the command: which $SHELL or echo $0 your terminal will reply with the shell you are currently running in. In Kali I get returned /usr/bin/zsh The Zsh Shell includes featrues from bash,ksh and tcsh along with some original features. This shell is designed for interactive use and is very powerful but we will be focusing on BASH. BASH scripting is essentially just automating everything we want to type into the shell. You might also notice that there is a file path we are given when inside of the terminal. If you run the command pwd the terminal will reply back with the present working directory. This is where you are running your commands from. Lets create a script that prints this out for us. To do this there are many tools but today we will use nano, so go ahead and type in nano directory.sh and hit enter. Nano is a text editor much like notepad for windows, after this command we are naming the text file we are creating directory with the file extension sh or shell script. In the text editor on the first line enter in #!/bin/bash This is telling the interpreter we want to use bash for this script. On the next line enter in pwd and hit ctrl+x to close nano. You will be prompted to save the file select Yes and then prompted again to name the file and hit enter. To verify the script was saved in your current directory enter in ls and hit enter. You should see the file you just saved in the list of files in your current directory. To run your script enter in bash directory.sh and this will run the PWD command. To edit the file again enter in nano directory.sh this will bring up the text editor and the script we just wrote. Lets add in on the next line sleep 2 and on the line after that ls and save the script. You can use the up arrow to run through the list of previous commands we executed in this terminal. Lets run bash directory.sh again. This time it will print out the present working directory, wait 2 seconds and then list the files we can see here.
If we try to run our script using ./directory.sh we will get an error: permission denied. This happens because instead of calling bash we used ./ to run the script. This is telling the terminal that the script is in out present working directory but because this script does not have the executable permissions it can not be ran. Lets check the permissions on this file by running ls -l this will display the file permissions to far right of the termianl as -rw-r--r--. The rw- at the start shows us the basic file permissions, this means we can read and write to this file but we need to add the execute permission. To add this permission lets run chmod +x directory.sh The chmod command allows us to change the permissions of a file, +x adds the executable permission to the file name which is followed after this. After running this if we check ls -l we can see the permission is changed to -rwxr. Now if we run ./directory.sh we no longer get this permission denied error.