Using the bash shell
If you are serious about development or QA then you are going to be spending a lot of time on the command line.
Here is a quick tutorial that teaches you the most important commands that you will need in the bash shell.
We will be using a Mac to drive this tutorial. If you are using a Linux system you can still use all the commands but may need to modify your steps when we work with the File Manager.
What is the bash shell?
The bash shell is a terminal application on Unix systems where you can type and execute commands in a terminal window. If you have used the Windows Command Prompt, it is similar to that.
The bash shell is used on both Mac and Linux operating systems. The commands between the two are 99% the same, so you shouldn’t run into too many issues when using the same command between the two.
For this tutorial, we are going to be using the terminal on a Mac.
Starting the terminal
On your Mac, use the command+space shortcut to open the search. Type in “terminal” and choose the top result that returns. On Linux you will likely see it under Applications >> System or Application >> Utilities.
You should see something similar to this.
The first thing you see, other than that “Last Login” message at the top, is the username of the person currently logged into the Mac.
After the username is a “$” followed by a blinking indicator that “this is where you type”.
Do you notice the tilde (~) right after the colon? That tilde is a shortcut for the home directory for your user.
The bash terminal is case sensitive. What that means is that “ls” and “LS” are not the same.
Spaces in folders and files can cause problems for applications, so I typically recommend never having a space in file or folder names. However, you are likely going to find things with spaces in them.
Whenever you need to do something with a file or folder with a space in the name you need to escape the spaces. Escaping a space means that you want the space character to be treated as a text space. Typically in bash when you have a space that means that you are trying to pass in a flag or argument to your command.
Confusing? Don’t worry, we will walk through an example later.
Getting the full current path
So unlike the Windows command prompt, the bash terminal doesn’t show you the full current path as part of the prompt.
Type the command, “pwd” in the prompt.
For me, I am located at my user directory, which is the typical starting point after you launch the bash terminal.
Here is what I see:
Jasons-MacBook-Pro-3:~ jasonmyers18$ pwd /Users/jasonmyers18
So let’s compare this with the Finder. Go ahead and open the Finder app on your Mac or whatever file manager your Mac uses.
Navigate your Finder window to the same directory that you see in the terminal.
You are now at the same directory as the bash terminal. Taking a quick look around, you should see a bunch of folders and files.
Listing the folders and files
Let’s list out the folders and files in the bash terminal.
Type “ls” and hit the enter key.
You will see a list of folders and files that are inside of your current directory.
The “ls” command has some options that you can add.
ls [options] [location]
So remember how I said that the bash terminal treats the spaces in your command as arguments. This is the first example. The parts of the commands that have the square brackets [ ]around them are optional.
Try the command “ls -l”. Make sure there is a space right before that dash.
You can see the output is a lot different now. The results now include the file permissions, file size, last updated date. Also the output is structured in a more readable format.
In the terminal type “cd /”. Make sure you have a single space right after the “cd”. Hit that enter key.
Now your path in the terminal should have changed a bit.
Mine looks like:
I am now in the root directory.
Go ahead and type “ls” again and you will see a new list of files and folders. Type “pwd” to see the full path.
Now let’s go back to the home directory for my user. Type “cd ~” and hit enter. You should be back to where you started.
Remember how I said earlier that the “~” is a shortcut for your home directory, let’s prove that. Type “cd /Users/<username>”, replace the “<username>” with your username, then hit enter.
Enter the “pwd” command once again and you should see you are still on the same path.
Moving back through directories
So right now my current directory hierarchy would look something like this.
/ -- Users\ -- jasmyers18
How do I get back to the “Users” folder?
In the terminal type “cd ..” and hit enter.
You should now have moved back one folder. Let’s go ahead and move back to your home directory by typing “cd <username>” again. Type “pwd” to confirm your location.
Let’s try moving back two directories.
Type “cd ../..” and hit enter. You should now be here:
You can follow this pattern any number of times until you reach the root (/).
Let’s go back to where we started. Type “cd ~”.
In the terminal type “mkdir test” and hit enter.
You should see that folder appeared if you do an “ls” command.
Feel free to make more folders if you would like.
Note: The mkdir command, and several other commands may be restricted depending on where you are. If you try to execute “mkdir test” while in the “/” directory you will get a permission denied error. The reason is your user doesn’t have permission to execute that command. There are ways around this but will save that for another lesson.
Note: Before you continue here, make sure you are 100% sure you want to delete the folder. Folders and files can be critical files in making your Operating System work properly. Deleting these critical files could make your computer unusable.
So let’s delete that folder that we created earlier. Type “rmdir test”.
Run the “ls” command to confirm the folder was deleted.
Create a file
Let’s go ahead and create a new folder. Type “mkdir test” once again to re-create this folder.
Move into the test folder. You should remember how, or cheat by reviewing above.
Now type “vim test.txt” and hit enter. You have just activated the vim console text editor. Vim could have a tutorial on its own so I will just give you a few basic commands and get out of there.
Hit the “i” key. You will notice at the bottom of the console there is some text that says “– INSERT –“. You are now in editor mode and can add text to the file.
Type “Hello”. Hit the “esc” key to exit the editor mode. Not finally hold down the shift key and press “zz”. If you did that correctly you should have exited vim and be back on the console.
Do an “ls” command. You should not see the text file you just created.
Delete a folder with files
Wait, we already learned to delete a folder. Yes, but it is easy when there is nothing in the folder.
Move back to “~” and try to delete the test folder again.
rmdir: test/: Directory not empty
Warning once again to make sure you are deleting the correct thing before you proceed.
Now, let’s try deleting that folder again. Type “rm -r test” and hit enter.
Folder and the file inside it are now gone.
Let’s go ahead and create that “test” folder one more time.
Create a “test.txt” file inside of it.
Now while you are inside the Test folder create two more folders, test1 and test2.
Now while still in the test folder type “mv test.txt test1” and hit enter.
Go ahead and execute the “ls” command. You should now see the test.txt file is gone.
Jasons-MacBook-Pro-3:test jasonmyers18$ ls
Go ahead and move yourself to the test1 folder. Run the “ls” command again. You should see your .txt file there.
While still in the test1 folder type “cp test.txt test1.txt” and hit enter.
Run the “ls” command. You should now see 2 files in the test1 folder.
Now try typing “cp test.txt ..” and hit enter. Move back to the test folder and run the “ls” command. You should see the file you just copied.
Stay in the test folder and type “mv test.txt fun.txt” and hit enter. Run the “ls” command. You should see your file was renamed.
Jasons-MacBook-Pro-3:test jasonmyers18$ ls fun.txt test1 test2
You will notice that the “mv” command does double duty. You can use it to move or rename files.
That fun.txt file isn’t very fun so let us delete it. Type “rm fun.txt” and hit enter. Run the “ls” command and verify the file was deleted.
Now, what if you forget the things I taught you so far?
No worries, just type “help” and hit enter. You will get a list of commands that you can use.
Clearing the console: The console can get quite messy and sometimes you just want to wipe the screen clean. Type the command+k keys on a Mac or you can type “clear” and hit enter. You have a clean slate.
Command history: The Command Prompt remembers the last commands that you entered. You can easily recover these, as long as you don’t close the window. Simply hit the UP arrow on the keyboard and you will see the commands that you last ran. Keep hitting UP or DOWN until you find what you are looking for.
Auto-complete: When you are in a folder and want to quickly get the name of the folder or file in that directory without manually typing in the whole thing.
First thing you can do is hit that TAB key. You will see the names of the files and folders auto-populate in the prompt. Hitting TAB again will give you the next item in the current folder.
If there are a lot of folders and files you can give it the first few letters and then hit the tab key. You will now only get things that match the first letters you entered. Keep hitting TAB and it will cycle through the matching files.