5.3 Flags

Conventions

Whether or not a command takes flags, and what those flags are, is up to the developer of the command. That said there are some common conventions:

  • Single-character flags are prefixed with a single dash (.e.g -a)
  • Multi-character flags are prefixed with two dashes (e.g. --help)
  • Sometimes the same flag can be used with a single dash or two dashes (e.g. -h or --help)

5.6 Exit Codes

Exit codes (sometimes called “return codes” or “status codes”) are how programs communicate back whether they ran successfully or not.

0 is the exit code for success. Any other exit code is an error. 9 times out of 10, if a non-zero exit code is returned (meaning an error) it will be 1, which is the “catch-all” error code.

Programs that call other programs use error codes to figure out if execution was successful. For example, if the Boot.dev server program exits with a non-zero exit code, we have another program that will automatically restart it and log the error.

In a shell, you can access the exit code of the last program you ran with the question mark variable ($?). For example, if you run a program that exits with a non-zero exit code, you can see what it was with the echo command:

ls ~
echo $?
# 0
ls /does/not/exist
echo $?
# 1

5.7 Standard Output

“Standard Output”, usually called “standard out” or “stdout”, is the default place where programs print their output. It’s just a stream of data that prints to your terminal, but we’ll talk later about how it can be redirected to other places.

All programming languages have a simple way to print to stdout. In Python, it’s the print function:

print("Hello world")
# Hello world

In a shell, it’s the echo command:

echo "Hello world"
# Hello world

5.8 Standard Error

“Standard Error”, usually called “stderr”, is a data stream just like standard output, but is intended to be used for error messages.

It’s a separate stream so that you can redirect it to a different place if need be, but by default, it prints to your terminal just like stdout.

Redirecting Streams

You can redirect stdout and stderr to different places using the > and 2> operators. > redirects stdout, and 2> redirects stderr.

Redirect stdout to a File

echo "Hello world" > hello.txt
cat hello.txt
# Hello world

Redirect stderr to a File

cat doesnotexist.txt 2> error.txt
cat error.txt
# cat: doesnotexist.txt: No such file or directory

5.9 Standard In

“Standard Input”, usually called “standard in” or “stdin”, is the default place where programs read their input. It’s just a stream of data that programs can read from as they run.

All major programming languages provide a simple way to read from stdin. In Python, it’s the input function:

# execution stops until the user types
# something (in this case "Lane") and presses enter
name = input("What is your name? ")

print("Hello,", name)
# Hello, Lane!

5.13 Unix Philosophy

The Unix Philosophy is a simple set of principles that have guided the development of Unix-like operating systems for decades. It can be summarized as:

  1. Write programs that do one thing and do it well.
  2. Write programs to work together.
  3. Write programs to handle text streams, because that is a universal interface.

1. Write Programs That Do One Thing and Do It Well

This is why programs like lsgrep, and less exist. They do one thing, and they do it well. They don’t try to do too much.

  • ls lists files and directories
  • grep searches for text
  • less displays text

2. Write Programs to Work Together

Because, at least according to the Unix Philosophy, programs should do one thing and do it well, it’s easy to write programs that work together. For example, you can use grep to search for text in a file, and then pipe the output of grep into less to display the results interactively:

grep "hello" some_file.txt | less

3. Write Programs to Handle Text Streams, Because That Is a Universal Interface

This point is more the “how” of the previous point. Programs work together easily when they all use the same interface: text streams. A text stream is just a sequence of characters that can be read or written sequentially. In other words, a text stream is just text.

This hearkens back to the point we talked about at the beginning of this course: the shell is a command-line (text) interface. Text-based interfaces are much more powerful and extensible than graphical interfaces. That’s why developers have been using them for decades, and why what we can do with them looks like magic to the uninitiated.