The Shell

Using the shell

When you log into remote Eos, you will see a welcome screen like this with a prompt:

*-----------------------------------------------------------------------------*
|                                     |    @@@@@@@     @@@@@@@     @@@@@@@@   |
|                                     |   @@@   @@@   @@@   @@@   @@@         |
|   North Carolina State University   |   @@@   @@@   @@@   @@@   @@@         |
|            College of Engineering   |   @@@@@@@@    @@@   @@@    @@@@@@@    |
|         Eos Computing Environment   |   @@@         @@@   @@@         @@@   |
|                                     |   @@@         @@@   @@@         @@@   |
|                                     |    @@@@@@@@    @@@@@@@    @@@@@@@@    |
*-----------------------------------------------------------------------------*
|                                                                             |
| You have logged in to NCSU College of Engineering remote access servers,    |
| which provide computing resources to faculty and students working remotely. |
|                                                                             |
| If you need assistance, please contact eoshelp@ncsu.edu.                    |
*-----------------------------------------------------------------------------*
Last login: Tue May  6 08:38:30 2025 from 136.47.231.18
type "man eos" (without quotes) for information on software installed
Filesystem                      Size  Used Avail Use% Mounted on
dc2isi00nfs.oit.ncsu.edu:/home   20G  2.7G   18G  14% /mnt/ncsudrive
[ahaque3@engr-ras-207 ~]$
 

In the example above, the shell tells you that you are on the machine engr-ras-207 and that your “current working directory”, or where you currently are, is ~ (short for “home”). The $ tells you that you are not the root user. At this prompt you can type a command, which will then be interpreted by the shell. The most basic command is to execute a program:

[ahaque3@engr-ras-207 ~]$ date

Tue May  6 13:23:00 EDT 2025

Here, we executed the date program, which (perhaps unsurprisingly) prints the current date and time. The shell then asks us for another command to execute. We can also execute a command with arguments:

[ahaque3@engr-ras-207 ~]$ echo hello

hello

Here, we told the shell to execute the program echo with the argument hello. The echo program prints out its arguments. The shell parses the command by splitting it by whitespace. To provide an argument that contains spaces or other special characters (e.g., a directory named “My Photos”), you can either quote the argument with ‘ or ” (“My Photos”) or escape just the relevant characters with \ (My\ Photos). Better yet is to use camel case or snake case to differentiate between words (look it up if you’re interested!).

But how does the shell find the date or echo programs? The shell is a programming environment, like Python or Ruby, and so it has variables, conditionals, loops, and functions. To run commands in your shell, you write a small bit of code that your shell interprets. If the shell receives a command that doesn’t match one of its programming keywords, it consults an environment variable called $PATH. $PATH lists in which directories the shell should search for programs when it is given a command:

[ahaque3@engr-ras-207 ~]$ echo $PATH

/mnt/ncsudrive/a/ahaque3/.cargo/bin:/mnt/ncsudrive/a/ahaque3/.local/bin:/mnt/ncsudrive/a/ahaque3/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/mnt/ncsudrive/a/ahaque3/.dotnet/tools:/opt/puppetlabs/bin

When we run the echo command, the shell sees that it should execute the program echo and searches through the :-separated list of directories in $PATH for a file by that name. When it finds it, it runs it (assuming the file is executable; more on that later).

Navigating in the shell

Recall from File System Hierarchy that a path on the shell is a delimited list of directories; separated by / on Linux and macOS and \ on Windows. This class assumes that you are using a Linux filesystem, usually remote Eos. To see absolute paths, use the pwd command. Change paths with the cd command. In a path, . refers to the current directory, and .. to its parent directory:

[ahaque3@engr-ras-207 ~]$ pwd

/mnt/ncsudrive/a/ahaque3

[ahaque3@engr-ras-207 ~]$ cd AlicePractice/

[ahaque3@engr-ras-207 AlicePractice]$ pwd

/mnt/ncsudrive/a/ahaque3/AlicePractice

[ahaque3@engr-ras-207 AlicePractice]$ ls

file1.txt

[ahaque3@engr-ras-207 AlicePractice]$ cd ..

[ahaque3@engr-ras-207 ~]$ pwd

/mnt/ncsudrive/a/ahaque3

My shell prompt told me what my current working directory was. I have a directory called AlicePractice that I move into, and then I use .. to move back up.

In general, when we run a program, it will operate in the current directory unless we tell it otherwise. For example, unless a directory is given as its first argument, ls will print the contents of the current directory.


[ahaque3@engr-ras-207 AlicePractice]$ ls
file1.txt

Above, I am currently in my AlicePractice directory, which has one file named file1.txt in it. On the other hand, if I am in my home directory ~ then typing ls AlicePractice (i.e., giving an argument AlicePractice) lists out what’s in the AlicePractice directory.

[ahaque3@engr-ras-200 ~]$ ls AlicePractice/
file1.txt

Most commands accept flags and options (flags with values) that start with - to modify their behavior. For example, -l modifies the behavior of ls to give us more information:

  -l                         use a long listing format
[ahaque3@engr-ras-207 AlicePractice]$ ls -l

total 24

-rwxrwx---+ 1 ahaque3 NCSU 0 Oct 10  2024 file1.txt

Now we have more information about each file or directory present.

The first column in the ls -l output shows the file type and permissions. The first character indicates the file type:

  • - for a regular file
  • d for a directory
  • l for a symbolic link

Follow three groups of three characters (rwx). These represent the permissions the owner, group, and everyone else have on the relevant file:

  • r stands for read permission
  • w stands for write permission
  • x stands for execute permission
  • - means does not have permission

To enter a directory, a user must have “search” (represented by “execute”: x) permissions on that directory (and its parents). To list its contents, a user must have read (r) permissions on that directory.

To learn how a program works and its inputs and outputs, use the man program. It takes as an argument the name of a program and shows you its manual page. Press q to exit.

[ahaque3@engr-ras-207 AlicePractice]$ man ls

Connecting Programs

In the shell, programs have two primary “streams” associated with them: their input stream and their output stream. When the program tries to read input, it reads from the input stream, and when it prints something, it prints to its output stream. Normally, your keyboard is input and your screen is output to and from the terminal. However, we can also rewire those streams!

The simplest form of redirection is < file and > file. These let you rewire the input and output streams of a program to a file respectively:

[ahaque3@engr-ras-207 AlicePractice]$ echo hello > hello.txt

[ahaque3@engr-ras-207 AlicePractice]$ cat hello.txt 

hello

[ahaque3@engr-ras-207 AlicePractice]$ cat < hello.txt

hello

[ahaque3@engr-ras-207 AlicePractice]$ cat < hello.txt > hello2.txt

[ahaque3@engr-ras-207 AlicePractice]$ cat hello2.txt 

hello

In the example above, cat is a program that concatenates files. When given file names as arguments, it prints the contents of each of the files in sequence to its output stream. But when cat is not given any arguments, it prints contents from its input stream to its output stream (like in the third example above).

You can also use >> to append to a file. Where this kind of input/output redirection really shines is in the use of pipes. The | operator lets you use the output of one program as the input of another:

[ahaque3@engr-ras-207 AlicePractice]$ ls -l ~ | tail -n1

drwxrwx---+  2 ahaque3 NCSU      0 Jan 10  2023 Videos

Chaining

To execute multiple commands on the same line, you can “chain” them together using a semicolon.

Example: Copy a file to your home directory, then to go to your home directory and confirm that the file has copied.

eos$ cp filename ~/; ls ~/
  • Note: the commands will execute in the order they are typed. If you receive an error on the second command, that means that the first command executed successfully and the computer stopped executing after the error.
  • You may know && or || for conditional chaining. Conditional chaining is a separate topic from just chaining. If you are asked to chain it means use the ;.

Clear the screen

Type clear to scroll your prompt to the top of the screen — this makes it easier to read. You can use the Up key to read previous commands or scroll back.

Keyboard Shortcuts

  • Ctrl + c aborts the currently running processes in terminal and return you to the command prompt if the system is “hung up.” This command will also abort any log files, so avoid using this while the script command is running.
  • Ctrl + z stops a running application or process and places it in the background. You can go back to the application by running fg.

Next steps

We want you to be able to navigate around in the shell to find files and do basic tasks. We have more chapters in this text that expand upon the commands we explained here, with more examples.

Exercises

The exercises following either give you a specific task to do, or are open-ended, like “try using X and Y programs”. We encourage you to try them out.

We have not written solutions for the exercises. If you are stuck on anything in particular, message the teaching staff, describing what you’ve tried so far, and we will answer! There are two ways you can run terminal commands: locally or through a remote system, which for NC State users is Eos.

  1. Use your terminal locally: If you are on Linux or macOS, you don’t have to do anything special:  just open the terminal. If you are on Windows, do not run cmd.exe or PowerShell; you can use Windows Subsystem for Linux or a Linux virtual machine to use Unix-style command-line tools.
  2. To log onto the remote Eos system:
    • Windows user: Use PuTTY or PowerShell to ssh in. To make sure you’re running an appropriate shell, you can try the command echo $SHELL. If it says something like /bin/bash or /usr/bin/zsh, that means you’re running the right program.
    • For Linux or Mac users, ssh in using the Terminal.

Once you have the command prompt ready, you can attempt the exercises.

  1. Create a new directory called books under ~.
  2. Look up the touch program. The man program is your friend.
  3. Use touch to create a new file called dracula.
  4. Try to execute the file, i.e. type the path to the script (./dracula) into your shell and press enter. Does it work? Consult the output of ls to see why it would/wouldn’t work. (hint: look at the permission bits of the file).
  5. Look up the chmod program (e.g. use man chmod).
  6. Use chmod to make it possible to run the command ./dracula. How does your shell know that the file is supposed to be interpreted using sh? See this page on the shebang line for more information.
  7. Use | and > to write the “last modified” date output by semester into a file called last-modified.txt in your home directory.

 

Page modifies information from The Missing Semester of Your CS Education (https://missing.csail.mit.edu/). Copied freely thanks to the license: CC BY-NC-SA.