Introduction to the CLI

First Steps with the Shell

Santiago Álvarez Rodríguez
Front-end Dev at PSL
santiaro90@gmail.com

What is a Shell?

An interface provided to the user, so he/she can interact with the OS core services (kernel)

Can be a CLI

(Command-Line Interface)...

or GUI

Shell == Terminal?

Terminal

Shell

reads commands

prints results

Back in Ol' 70s

Terminal

Shell

reads commands

prints results

Now

Emulator

Shell Implementations

  • sh (Bourne Shell)
  • ksh (Korn Shell)
  • csh (C Shell)
  • tcsh
  • Bash (Bourne Again Shell)
  • zsh
  • fish (Friendly Interactive Shell)
  • etc., etc., etc.

Shell Types

Login

  • Started after a system login. (1)
  • Sources the following files (2):
    • /etc/profile
    • ~/.bash_profile OR ~/.bash_login OR ~/.profile (3)
  • Allows finishing the shell with the logout command.
  • On logout, sources ~/.bash_logout.
  1. A login shell can be started interactively with bash --login.
  2. Bash-specific.
  3. Searches files in that order and sources the first one it finds, ignoring the rest.

vs.

Non-Login

  • Started by the user or as a sub-shell.
  • Only sources ~/.bashrc. (2)
  • Uses the exit command to finish the shell.

Interactive

vs.

Non-Interactive

Reads commands from user input.

Reads commands from a file (script).

Basic Commands

echo
~ $ echo 'Hello world!'
Hello world!


~ $ echo 'Hello\nworld!'
Hello\nworld!


~ $ echo -e 'Hello\nworld!'
Hello
world!


~ $ echo '$HOME'
$HOME


~ $ echo "$HOME"
/home/santiaro90
cat
# Don't worry about what '>' means, for the moment...
~ $ echo -e 'Hello\nworld!' > hello.txt
~ $ echo 'Hola Mundo' > hola.txt


~ $ cat hello.txt
Hello
world!


~ $ cat hello.txt hola.txt
Hello
world!
Hola Mundo


~ $ cat -n hello.txt
    1. Hello
    2. world!
cd (Change Directory)
# Go to ~/Documents/Important...
~ $ cd ~/Documents/Important
~/Documents/Important $


# Go to $HOME (same as ~)
~/Documents/Important $ cd
~ $


# Take us back to the last directory we were in
~ $ cd -
~/Documents/Important $


# We can use relative paths :)
~/Documents/Important $ cd ./.././Important/../../Downloads
~/Downloads $
alias
~ $ alias web-projects='cd /home/santiaro90/Projects/Web; echo "Have a nice coding!"'
~ $ web-projects
Have a nice coding!
~/Projects/Web $


~ $ unalias web-projects
~ $ web-projects
-bash: web-projects: command not found
Other commands
# See documentation for a given command
~ $ man <command>


# Print current directory
~ $ pwd
/home/santiaro90


# See information for current user
~ $ who am i -H
NAME        LINE    TIME                COMMENT
santiaro90  pts/0   2016-08-25 11:35


# See information for all logged in users
~ $ who -H
NAME        LINE    TIME                COMMENT
santiaro90  pts/0   2016-08-25 11:35    (192.168.56.1)
root        tty2    2016-08-25 11:28
santiaro90  :0      2016-08-25 12:10


# Print current date
~ $ date
Sun 28 Aug 18:58:28 COT 2016

Some Links

Introduction to the CLI: First Steps with the Shell

By Santiago Álvarez Rodríguez

Introduction to the CLI: First Steps with the Shell

Short definition of shell, how is it different from a terminal, kinds of shells and basic Bash commands.

  • 1,056