The terminal (and Bash) are some of the most useful tools you will learn to use in your CS career. If you're new to programming, they can appear daunting at first, but once you get the hang of them, you will be able to perform a variety of functions in the terminal using only text.
This guide is meant to serve as a primer to get you started. Your instructor or TA(s) should be able to help you with using the terminal to work on your projects, or you can always Google what you need help with to try to figure it out yourself!
What is the terminal?
The terminal is the area highlighted by the orange box above. This is where you will be typing in bash command.
If you close your terminal window you can open a new one using the file menu.
What is Bash? What is a REPL?
Bash is a type of shell for the Unix-type operating system. Shells are simple interactive computing environments (also known as a REPL), generally text only, that can be used to take user input, evaluate it, and return the results to the user.
REPL is an acronym for Read, Evaluate, Print, and Loop:
Read user input from the terminal
Evaluate the user input, running commands and generating the output
Print the results in the way that the user or shell specifies
Loop back and ask for user input again
REPLs exist for many different languages, and for many different purposes.
The way we will use Bash is by calling commands in order to accomplish what we want.
We can accomplish this by typing in our commands, and hitting the Enter key to execute the line.
Let's look at what the basic command structure looks like:
Basic Command Structure
All commands that you will run via the command line interface will follow a similar structure:
command [-options] {arg0 ... argN} (< input) (> output) (| pipe?)
Let's break those down:
command:
The actual command you are running. This can be a variety of words and phrases depending on the operating system you use.
# examples:
clear
# 'clear' clears the current screen
ls
# 'ls' lists the current directory and the files within it
pwd
# 'pwd' prints the name and location of the working directory
options:
These are options (also known as flags) that cause a command to run slightly differently. This can cause many different things to happen based on the options you select, check the man pages for the command for more info.
# example: -alF
ls -alF
# '-alF' on the 'ls' command corresponds to the following flags:
# -a: all files, including hidden ones
# -l: long listing. shows more data
# -F: classify listings
# man pages http://man7.org/linux/man-pages/man1/ls.1.html
arg0 ... argN:
These are the arguments passed to the command (argument 0 through argument N), usually a single argument but can be a large list of arguments. These are the main variables passed to the command. These are separated by spaces.
# example:
cd /home/mimir/project1
# '/home/mimir/project1' is the argument to the 'cd' command
input:
This allows input redirection, meaning you can use the contents of a file as the input for the command. It is useful for a variety of reasons that we will look into later.
# example:
python process_data.py < data.txt
# this runs python, with the script called 'process_data.py'
# it also passes the 'data.txt' file in as the input to the script
output:
This allows output redirection, printing what would normally go to the terminal log into a file with the given name.
# example:
python run_calculation.py > results.txt
# this runs python, passing in a script called 'run_calculation.py'
# it also makes the output write to a file called 'results.txt'
# output will not be printed to the terminal.
| pipe:
The pipe character ( | ) redirects the output of the command to the next command in the chain. This becomes very useful when we begin using new commands as well as chaining them.
# example:
python read_file.py | tail -5
# this runs python, passing in a script called 'read_file.py'
# the output is piped to the tail command, which prints the last N lines
# -5 sets N to be 5, so the last 5 lines of the output are printed out
Basic Commands
Now that you understand a bit about the basic command structure, here are a few basic commands that you can use.
General
echo <text ...>
The echo command allows you to print something out to the terminal
# example:
echo Hello World!
clear
The clear command clears all of the text from your terminal screen.
# example:
clear
help
The help command will bring up the basic Bash help text.
# example:
help
man <command>
The man command will bring up a manual for a given command. This can be very useful for exploring new commands, as it will show flags and arguments you can pass to the program! Example:
# try this!
man python
File System and Movement
cd <directory>
The cd (Change Directory) command will change your working directory to the directory you pass in as an argument. Special operators for cd (and other file structure commands) include:
. - this single dot stands for the current working directory
.. - these two dots stand for the parent directory
~ - the tilde expands to be the home directory of the current user
- - the dash expands to the last working directory you were in
# examples:
cd /home/mimir
# this changes the working directory to '/home/mimir'
cd ..
# this changes the working directory up a level
cd ~
# this changes the working directory to your home directory
pwd
The pwd (Print Working Directory) command will print the path of the directory that your terminal is currently in
# example:
pwd
ls
The ls (LiSt) command will list files in the working directory
# example:
ls
mkdir <directory>
The mkdir (MaKe DIRectory) command will make a directory with the name that you pass in as an argument
# example:
mkdir project1
# this makes a directory called 'project1'
cp <file0> <file1>
The cp (CoPy) command will copy a file from file0 to file1, overwriting the file in the file1 location if it exists
# example:
cp test1.py test2.py
# this copies the 'test1.py' file to 'test2.py'
mv <file0> <file1>
The mv (MoVe) command will move a file from file0 to file1, overwriting the file in file1 location if it exists. You should use this command to rename files!
# example:
mv old_file.txt new_file.txt
# this moves 'old_file.txt' to 'new_file.txt', effectively renaming it
rm <file0>
the rm (ReMove) command will remove a file, given by the file0 argument. Use the '-r' flag to remove directories!
# example:
rm old_file.txt
# this removes a file called old_file.txt
cat <file0>
The cat command will print the contents of file0 to your terminal
# example:
cat data.txt
# this prints the contents of a file called 'data.txt'
more <file0>
The more command will print a limited amount of file0 to your terminal, and give you controls to move through the file, scrolling through it as you hit enter or spacebar.
# example:
more data.txt
# this starts reading a file called 'data.txt'
head <file0>
The head command will print the first 10 lines of a file to the terminal
# example:
head long_file.txt
# this prints the first 10 lines of a file called 'long_file.txt'
tail <file0>
The tail command will print the last 10 lines of a file to the terminal
# example:
tail log_file.txt
# this prints the last 10 lines of a file called 'log_file.txt'
curl <url0>
The curl command will fetch a webpage or web resource given by url0, and will print the data to the terminal. You can use output redirection to put this data in a file!
# example:
curl https://www.google.com/favicon.ico > google.ico
# this will save the google favicon to a file called google.ico
Now that we have some basics down, how do I run my program?
Because Mimir Classroom supports so many languages and frameworks, there is no single solution for running a program. There are many ways to run each type of program, and each instructor may have a different preference for which method(s) to use - so your best bet is to ask your instructor.
With that being said, here are some very basic examples (CAREFUL! This may or may not be how your instructor wants you to run it!) for how to run certain language types on the MimirIDE:
Java 8
# if your file is named Program.java:
javac Program.java
java Program
Python
# if your script is named foobar.py:
# python 2
python2.7 foobar.py
# python 3.6
python3.6 foobar.py
# python 3.7
python3.7 foobar.py
# python 3.8
python3.8 foobar.py
C
# if your file is named main.c:
gcc main.c
./a.out
C++
# if your file is named main.cpp:
# std99
g++ main.cpp
./a.out
# std11
g++ -std=c++11 main.cpp
./a.out
# std14
g++ -std=c++14 main.cpp
./a.out