Introduction to the Shell

The content of this page is mainly based on MIT Missing Semester.

The course: Bash & mostly linux

Some Simple Commands

date
echo hello
echo "Hello world"

Space seperates parameters.

Environment

The computer has built-in programs. It also know where to find programs to execute for a certain command.

Environment variables are something that are set whenever you start your shell. One of them is $PATH. It determines where your device will search for programs. The device will go through the paths listed in $PATH and try to find the program whose name match the input.

echo $PATH

There is a useful command to tell which program will be used by the shell.

which echo

Path

absolute / relative path (omitted)

  • pwd: print working directory
  • cd: change directory
pwd
cd /home

Some special directories:

  • .: current path
  • ..: the parent directory
cd ./home
../../../bin/echo world

To list all the things in a directory (the current directory by default):

ls
ls ..

Some shortcuts:

  • ~: the home directory of the user
  • -: the directory you are previously in (this is useful if you want to toggle between two directories)
cd ~
cd -

Flags and arguments

Most programs support the help option:

ls --help

You can find a -l flag, which gives you more information.

ls -l

Permissions

In ls -l, you will find something like -rw-r--r-- or drwxr-xr-x. The meanings are as follows:

  • The first d means it is a directory. If - then it is a file.
  • The first group of 3 chars represents the permissions of the owner of the file.
  • The second group of 3 chars represents the permissions of the group that owns the file.
  • The final group of 3 chars represents the permissions for anyone else.

The owner and the group owner are displayed after the chars.

rwx means read, write, execute respectively. They have different meanings for files and directories.

rwx
FileRead the file contentWrite content to the fileExecute the file
DirectorySee the files in the directoryRename, create or remove files in the directorySearch, basiclly enter the directory (eg. by cd)

A dash - means you do not have that permission.

Suppose we have a file that looks like /home/text.txt. If you have w permission for the file text.txt but not the directory /home, then you can empty the file, but not delete it.

More commands

To move, copy or remove a file:

mv file1.md file2.md
cp file1.md ../file2.md
rm file1.md

You can remove a directory and all its content by adding a -r flag. You can also use rmdir, but it only allow you to remove the directory if it is empty.

rm -r dir1
rmdir dir1

Create directory. The first line creates 2 directories, while the second and third create 1 only.

mkdir dir1 dir2
mkdir "Hello World"
mkdir Missing\ Semester

Get a manual of a program

man ls

Tip: press Q to quit the manual program

Stream

A program read input and print output from/to streams. By default, the in/out streams are your console, but you can change it using < and >.

echo < f1.txt > f2.txt

Now the content in f1.txt becomes input and the output will be in f2.txt. We can print the content of a file using cat.

cat f2.txt

You can also append the output to the end of a file using >>, so that the original content will be preserved.

Pipe

A pipe | takes the output of the left command and use it as input to the program on the right.

ls -l / | tail -n1

The command above lists all things under /, and use tail to get the last line of the output.

Note that the programs executed know nothing about <, > and |. They just take the input and give the output.

An interesting example:

curl --head --silent google.com | grep -i content-length | cut --delimiter=' ' -f2

The root user (mainly on linux/MacOS)

A root user, or a superuser, can do anything they want. You can use sudo to run a command as the root user.

Something interesting:

sudo echo 500 > important_file

You will get permission denied, because the output of sudo echo 500 are used as input, and > is run by the shell. However, the shell is not running as root.

To open a root shell:

sudo su

Now the sign before the command should turn from $ to #, where the former means not running commands as root user, and the latter means the opposite.

Now you can run the commands below without error, because the shell itself is running as root.

echo 500 > important_file
sudo echo 500 > important_file

However, you can run this even in a non-root shell:

echo 1060 | sudo tee important_file

where tee write the input into a file and print the content out at the same time. (useful for logs)

Others

This will open a file with the suitable software. (Only on Linux)

xdg-open some_file.abc

Exercise

(P4) Create a file

touch a_file

(P5-9)

touch semester
echo \#\!/bin/sh >> semester
echo curl --head --silent https://missing.csail.mit.edu >> semester
chmod 700 semester
./semester
./semester | grep last-modified > last-modified.txt