Other Interfaces

Changing context in Terminal

We've talked a lot about how the command line is an interface to the files and directories on your computer, and how your current directory and environment variables provide context for what you do on the command line. There are some commands that you'll use on the command line that will completely change the context of the command line. In fact, these commands provide entirely new tools that can help you to interface more directly with files, to query databases, to experiment with scripting languages such as Ruby, JavaScript, and Python, or to otherwise manage your system. The following are the types of commands that change the context of the command line and provide new ways to interface with your computer:

  • Database management commands: mysql, psql, redis-client, mongo
  • Text editors: vim, pico, nano, emacs
  • REPLs (Read-Eval-Print-Loop), which are basically interactive scripting consoles: irb, python, php -a
  • System monitoring: top, htop
  • Reading files or manuals: man, less, more
  • Window/Session handling: byobu, screen, tmux

Each of the commands above provide an entirely new interface. Once you enter that interface, though, you'll have to exit out of it (usually by typing exit) to get back to the normal command line.

One very useful command that takes over the CLI display is top. When you run top, it takes over all of the available screen real estate and refreshes every few seconds:

$ top

The display and available commands have varied considerably over time, and they vary from OS to OS, often by version. Thus, we can't go into a lot of detail. However, you should see a display that looks something like this:


It probably won't be an exact match -- it may not even be a close match -- but it will look a bit like that. If you want to know more about what you're seeing and how you can modify the output, use the man command:

$ man top  # If this doesn't work, try using Google for your specific OS and version

Type q to exit top.

On a Mac, you can also use the Activity Monitor app from Applications/Utilities to see a similar information display in your GUI. The Activity Monitor is usually easier to read than top.

The mysql command, which is used to connect to and interface with a MySQL server, also takes over the command line interface. As you can see in the example below, once you have connected to a MySQL server, you'll see a new prompt (this will only work if you have mysql installed):

$ mysql -u root -p
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.6.13 Source distribution

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.


The prompt is now simply mysql>, and commands such as ls, echo, and cd no longer work. Instead, only SQL and other MySQL commands are available for use. The mysql program essentially hijacks the command line session until you exit out using the QUIT command.

Connecting to a MySQL server with the mysql command allows you to query databases and otherwise manage the database server. Just like the regular command line interface, there is input (STDIN) and output (STDOUT). If you type SHOW DATABASES;, it will print to the screen a list of databases on the server. If you type SELECT 1 + 1;, it will output a table with the result of 1 + 1.

To get back to the command line interface, type 'QUIT' and hit enter.

Read-Eval-Print-Loop (REPL)

Programs that follow the pattern of reading user input, evaluating the input, printing results of the input to the screen, and then allowing for more input, are called "REPLs", or "Read-Eval-Print-Loop" programs or interfaces. The mysql command provides a simple REPL. The bash shell, which is another name for the command line interface that we've discussed in this book, is also a REPL. Interactive interfaces for scripting languages such as Ruby, JavaScript, Python, and Perl are commonly referred to as REPLs—even more so than database clients and command line shells. These REPLs read user input, evaluate it using the scripting language's interpreter, print the result of the programming statement, then provide a prompt for more input.

Ruby REPL (irb)

If you don't have Ruby on your system, the commands in this section won't work. That's okay: you don't need Ruby right now.

In your console, try using the irb REPL. If Ruby is installed, you should see something like this:

$ irb

Just as with the mysql program, irb provides a prompt. As you can see in the following image, the irb program evaluates input, then performs what the input tells it to do, then outputs the result of what it did:

$ irb
irb(main):001:0> a = 1
=> 1
irb(main):002:0> puts "Hello, world!"
Hello, world!
=> nil

If you type a multi-line script, it will automatically postpone evaluating the input until the statement is finished:

$ irb
irb(main):001:0> a = 1
=> 1
irb(main):002:0> if a == 2
irb(main):003:1>   puts "A is 2"
irb(main):004:1> else
irb(main):005:1*   puts "A is not 2"
irb(main):006:1> end
A is not 2
=> nil

Type exit to leave irb.

JavaScript REPL (node)

If you don't have Node on your system, the commands in this section won't work. That's okay: you don't need Node right now.

In your console, try using the node REPL. If node is installed, you should see something like this:

$ node
Welcome to Node.js v12.10.0.
Type ".help" for more information.

As with the mysql program, node provides a prompt. As you can see in the following code, the node program evaluates input, then performs what the input tells it to do, then outputs the result of what it did:

$ node
Welcome to Node.js v12.10.0.
Type ".help" for more information.
> a = 1
> console.log("Hello, world!")
Hello, world!

If you type a multi-line piece of code, it will automatically postpone evaluating the input until the statement is finished:

$ node
> a = 1
> if (a === 2) {
...   console.log("A is 2");
... } else {
...   console.log("A is not 2");
... }
A is not 2

Press Control-D or Control-C to leave node.

Other Languages

Other languages, such as Haskell, Python, and PHP, also provide REPLs. As you learn to program, you will find that REPLs are very useful for experimenting with what you learn and for performing small computational tasks.


Some programmers use the command line as their IDE (Integrated Development Environment). Part of that IDE is the text editor, of which there are several available for the command line. The following interfaces may look familiar:




The above screenshots are all of text editors that are often used on the command line. When you see other developers working in interfaces like the ones above, they are probably not in the main shell REPL (the command line), and are instead using a text editor program within the command line.

Note: If you start typing command line commands while in an editor, those commands may be treated as normal text, or may be treated as editor commands. Watch out for that! You must exit the editor mode, and be back in the command line to issue command line (or "bash") commands.

The process of exiting is different for each of the editors mentioned above. Below is a list of the commands to exit each editor:


Exit and save:

<ESC> + :wq

Exit without saving:

<ESC> + :q!


Exit and save:

<Ctrl> + o then <Enter>, then <Ctrl> + x

Exit without saving:

<Ctrl> + x then n to discard changes


Exit and save:

<Ctrl> + x, <Ctrl> + s, then <Ctrl> + x, <Ctrl> + c

Exit without saving:

<Ctrl> + x, <Ctrl> + c, then n, <Enter> and yes, <Enter> to discard any changes.

Bash: a Special Interface

Sometimes, you may end up in a situation where you're working in a "command line within a command line". This is because the command line interface we've been working in is itself a special interface that can be nested. For example, while using the command line you may think that you're in the normal command line interface, but when you type exit and hit enter, you remain in what appears to be another CLI. This is most likely because you've unknowingly entered another bash (or shell) interface. When on the command line, typing bash, zsh, or similar will create a new nested shell interface for you to work in. You can exit the innermost "shell" by typing exit. When you're in the outermost shell and you type exit, you'll see something like [Process completed] without a way to enter more commands. You'll have to close and restart Terminal at this point and start a new shell. If you're working normally, you should very rarely encounter a nested command line situation, but it can happen.


When you open up Terminal and start using it, you may not always be using the default command line: bash. Programs like mysql, vim, and irb provide interfaces of their own right within Terminal. It's very important to understand whether you're in an application-specific REPL, like mysql, or if you're in the bash shell REPL (aka, the command line). Sometimes, the only clue you have is the prompt. But as we've seen earlier, we can even modify the prompt by setting the PS1 environment variable, so it can be quite tricky.