Unix shell basics: Practical guide for Developers

Being comfortable with the shell is one of the most useful skills a developer can build. As a key component of Unix-based operating systems, the shell is still essential for automation and many everyday development tasks. This article is a short, practical guide covering basic shell concepts.

Santiago Marulanda

Santiago Marulanda

Author · Tiagocode

Operating systems💻
Unix shell basics: Practical guide for Developers

What is a shell

As mentioned in the introduction, the shell is one of the most important pieces of software in Unix-based operating systems.

It is a program that allows the user to interact with the system by running commands. Those commands can be external programs or built-in features provided by the shell itself.

Unix-like systems include a shell as a standard part of the operating system. Today, many different shells are available, such as  `sh`, `bash`, and `zsh`. Many of them follow the Bourne Shell style or are compatible with it, which is why Bourne Shell syntax is still an important reference point.

Different operating systems choose different default shells depending on their design decisions, licensing, and goals. For example:

  • macOS uses Z shell (`zsh`) as the default login shell.
  • Ubuntu commonly uses Bourne-Again shell (`bash`) for interactive sessions, although `/bin/sh`usually points to `dash`.

Shell vs Terminal 🤔

Now that the shell is defined, it is easier to understand the terminal and how both concepts are connected (they are often confused, but they are not the same).

A terminal is an application window that gives the user access to a text-based interface, such as GNOME Terminal, iTerm, or Alacritty.

Inside the terminal, a shell runs and interprets the commands entered by the user. In practice, every time a terminal application such as iTerm is opened, it starts a shell session inside that window.

iTerm terminal app

Understanding I/O Streams

Before looking at basic shell commands, it is important to understand the concept of an I/O stream. In Unix-like systems, processes use input streams to read data and output streams to write data. When a program is executed, the operating system creates a process and attaches the standard I/O streams to it. These streams allow the process to read input and write output.

In a simple terminal session, these streams are usually connected to the keyboard and the terminal display. However, they can also be redirected to files, devices, or other processes.

There are three standard streams:

Standard input (`stdin`)

This is the stream from which a program reads input data. In a terminal session, it is usually connected to the keyboard. Its file descriptor is `0`.

In the following example, `cat` reads input from `stdin`. Since `stdin` is connected to the keyboard in a normal terminal session, the command waits for user input and then prints it back to the screen.

tiagocode@raspberrypi ~ % cat
Hello from the command cat connected to stdin
Hello from the command cat connected to stdin

Standard output (`stdout`)

This is the stream to which a program writes its normal output. In a terminal session, it is usually connected to the terminal display. Its file descriptor is `1`. 

In the following example, the `echo` command writes the message "Hello World" to standard output.

tiagocode@raspberrypi ~ % echo "Hello World"         
Hello World

Standard error (`stderr`)

It's the stream to which a program writes error messages or diagnostic output. It separates from `stdout`, allowing normal output and errors to be handled independently. Its file descriptor is `2`.

In the following example, the `ls` command writes an error message because the target directory doesn't exist. Although this message is displayed in the terminal, it is written to standard error, not the standard output. This separation makes it possible to redirect the messages to a different file if needed (like log files)

tiagocode@raspberrypi ~ % ls /directory-that-doesnt-exists
ls: /directory-that-doesnt-exists: No such file or directory

Basic commands

Now it is time to open a terminal and try some basic commands. To follow this section, a terminal application is needed, such as iTerm, GNOME Terminal, Warp, Kitty, etc. There are many options available, so choose the one that best fits your style and workflow.

The examples cover common commands and basic concepts that are useful in everyday development.

Shell and environment variables

Like many programs, the shell can store temporary variables (called shell variables). These variables are very useful when creating scripts, and it is important to notice that some variables could also modify the behavior of the shell.

The next example defines a shell variable called `MYVARIABLE` that stores the value of  `Hello World!`. Then, the `$` character is used to read the value stored in the variable `$MYVARIABLE`.

tiagocode@raspberrypi ~ % MYVARIABLE="Hello World!"
tiagocode@raspberrypi ~ % echo $MYVARIABLE
Hello World!

On the other hand, environment variables are closely related to shell variables, but they serve a broader purpose. 

A shell variable exists only inside the current shell. After it is exported, it becomes an environment variable, which means it can be inherited by a child process started from that shell.

The following example uses the `export` command to turn a shell variable into a variable.

tiagocode@raspberrypi ~ % MYVARIABLE="Hello World!"
tiagocode@raspberrypi ~ % export MYVARIABLE

List directory contents (ls)

This is probably one of the most commonly used shell commands in everyday work. The `ls` command lists the contents of a directory.

In the following example, the `-l` option is used to display the output in long format, which includes additional details such as permissions, owner, size, and modification date.

tiagocode@raspberrypi ~ % ls -l
drwxrwxr-x 2 tiagocode tiagocode 4096 Dec  4 15:51 ExampleDirectory
-rw-rw-r-- 1 tiagocode tiagocode   56 Mar 30 00:37 example_file_1

The output of ls -l can be read from left to right:

  • drwxrwxr-x: file type and permissions
  • 2: number of hard links
  • tiagocode: owner
  • tiagocode: group
  • 4096: file size in bytes
  • Dec  4 15:51: last modification date
  • ExampleDirectory: file or directory name

The first character of the permissions field indicates the file type:

  • d means directory
  • - means a regular file

Create files (touch)

The `touch` command is mainly used to update the timestamps of a file. However, if the target file doesn't exist, `touch`creates a new empty file. If the file already exists, the command updates its timestamps instead of changing its content.

In the following example, the file is created the first time the command is executed. When the same command is run again, the file already exists, so only the timestamp is updated.

tiagocode@raspberrypi ~ % touch my_new_file_1
tiagocode@raspberrypi ~ % ls -l
drwxrwxr-x 2 tiagocode tiagocode 4096 Dec  4 15:51 ExampleDirectory
-rw-rw-r-- 1 tiagocode tiagocode   56 Mar 30 00:37 example_file_1
-rw-rw-r-- 1 tiagocode tiagocode    0 Apr  1 22:02 my_new_file_1

tiagocode@raspberrypi ~ % touch my_new_file_1
tiagocode@raspberrypi ~ % ls -l
drwxrwxr-x 2 tiagocode tiagocode 4096 Dec  4 15:51 ExampleDirectory
-rw-rw-r-- 1 tiagocode tiagocode   56 Mar 30 00:37 example_file_1
-rw-rw-r-- 1 tiagocode tiagocode    0 Apr  1 22:03 my_new_file_1

Current working directory (cd and pwd)

An important concept when working in the shell is the current working directory. This is the directory where the shell session is currently located. Many commands use it as the default reference point when working with files and directories.

The `pwd` command prints the current working directory, while the `cd` changes it.

Another important concept here is the home directories. This is the default personal directory assigned to a user account. In Unix-like systems, the home directory for the `root` user is usually `/root`. For regular users, it is typically located at `/home/{username}`.

In the following example, you'll see a useful shortcut for moving directly to the current user's home directory using the `cd` without arguments, or `cd ~`. In this context, the `~` character represents the user's home directory.

tiagocode@raspberrypi ~ % cd
tiagocode@raspberrypi ~ % pwd
/home/tiagocode
tiagocode@raspberrypi ~ %
tiagocode@raspberrypi ~ % cd ~
tiagocode@raspberrypi ~ % pwd
/home/tiagocode

The following example shows how the `cd` command can be used to move to another directory. In this case, it is used to move into `ExampleDirectory`. 

tiagocode@raspberrypi ~ % ls -l
drwxrwxr-x 2 tiagocode tiagocode 4096 Dec  4 15:51 ExampleDirectory
-rw-rw-r-- 1 tiagocode tiagocode   56 Mar 30 00:37 example_file_1
-rw-rw-r-- 1 tiagocode tiagocode    0 Apr  1 22:02 my_new_file_1
tiagocode@raspberrypi ~ % cd ExampleDirectory
tiagocode@raspberrypi ~ % pwd
/home/tiagocode/ExampleDirectory

View processes (ps)

Every command executed in the terminal, every application that is opened, and many background services started by the operating system are represented as processes. 

The `ps` command is used to display information about running processes. It's a very flexible command with many available options. One detail to keep in mind is that `ps` supports option styles, which can make its syntax look a bit unusual at first. In this guide, the examples follow BSD-style syntax (options are written without a leading dash and may be grouped together).

When `ps` is executed without any options, it shows only the processes associated with the current terminal session. In this case, the output includes the current shell (`zsh`) and the `ps` command itself.

The columns show the process ID (`PID`), the terminal associated with the process (`TTY`), the accumulated CPU time (`TIME`), and the command name.

tiagocode@raspberrypi ~ % ps
  PID TTY          TIME CMD
76817 pts/2    00:00:00 zsh
76822 pts/2    00:00:00 ps

When `ps` is executed with the `aux` options, it displays a broader view of the processes currently running on the system. This format also includes more columns and more detailed process information. Since the full output can be quite long, only a small portion is shown below.

tiagocode@raspberrypi ~ % ps aux
USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root       76927  0.0  0.0      0     0 ?        I    00:10   0:00 [kworker/u16:1-v3d_render]
root       76930  0.0  0.0      0     0 ?        I    00:11   0:00 [kworker/2:0]
root       76935  0.0  0.0      0     0 ?        I    00:11   0:00 [kworker/u19:1-kvfree_rcu_reclaim]
tiagoco+   76955  400  0.0   9232  3856 pts/2    R+   00:13   0:00 ps aux
  • `ps x` shows the user's running processes.
  • `ps ax` shows running processes of all users.
  • `ps u` adds detailed information about the processes.

Search patterns in files (grep)

It's common to need to search whether a file, or a group of files, contains a specific text. It is also common to search for specific messages in log files while inspecting their contents. The `grep` command is very useful for this, because it searches for the lines that match a given pattern.

In the following example, `grep` is used to search for the text `tiagocode` inside the `/etc/passwd` file. After running the command, the matching line is printed.

tiagocode@raspberrypi ~ % grep tiagocode /etc/passwd
tiagocode:x:1000:1000::/home/tiagocode:/bin/zsh

The next example shows how to print the contents of a log file with `cat` and filter only the lines that contain the text "error".

tiagocode@raspberrypi ~ % cat /var/log/*.log | grep error
W: An error occurred during the signature verification.

`grep` also supports regular expressions. For example, if both `root ' and `tiagocode` need to be searched, the `-E` option can be used to enable regular expression search.

tiagocode@raspberrypi ~ % grep -E 'root|tiagocode' /etc/passwd
root:x:0:0:root:/root:/bin/bash
tiagocode:x:1000:1000::/home/tiagocode:/bin/zsh

Get help (man)

Many commands support more options than those shown in the basic examples. To read more about a command and its available options, the `man` command can be used. It opens the manual page for the requested command.

The example below shows the manual page for `ls` (only a short portion is included here).

tiagocode@raspberrypi ~ % man ls
LS(1)                                                                     User Commands                                                                     LS(1)

NAME
       ls - list directory contents

SYNOPSIS
       ls [OPTION]... [FILE]...

DESCRIPTION
       List information about the FILEs (the current directory by default).

Command line shortcuts

When working in the command line, many actions are faster when performed directly from the keyboard. The following shortcuts help with command editing, navigation, control, and history, making the terminal work faster and more efficiently.

KeystrokeTypeAction
CTRL+BNavigationMove the cursor one character to the left (⬅️)
CTRL+FNavigationMove the cursor one character to the right (➡️)
CTRL+PNavigationShow the previous command in the history (⬆️)
CTRL+NNavigationShow the next command in the history (⬇️)
CTRL+AHistoryMove the cursor to the beginning of the line
CTRL+EHistoryMove the cursor to the end of the line
CTRL+WEditDelete the previous word
CTRL+UEditDelete from the cursor position to the beginning of the line
CTRL+KEditDelete from the cursor position to the end of the line
CTRL+YEditPaste back the last deleted text
CTRL+CControlStop the current running command or process
CTRL+DControlExit the current shell session, or send end-of-input if line is empty
CTRL+LControlClear the terminal screen 🧹
TABControlAutocomple commands, file names, and directory paths.

Final thoughts

You have reached the end of this article. Even though this guide was intentionally kept simple, it covered several useful concepts, including shell vs terminal, I/O streams, basic commands, and command-line shortcuts.

I hope you enjoyed reading it. A good amount of time and care went into writing it and selecting the concepts that felt most useful for a practical introduction. More content may be added in the future, especially in basic commands, as the article continues to grow over time.

If you made it all the way here, thank you for reading to the end. That is genuinely appreciated. And if this article was helpful, feel free to leave a comment or get in touch.