[LCLB] L01 - The Shell
contents
user interfaces
- CLI - Command Line Interface
- GUI - Graphical User Interface
terminal
- the terminal originally meant a physical piece of interface hardware needed to interact with the computer
- it was a replacement for punch cards
- live input and output with computer program was possible with the terminal
- punch cards were fed into the computer before the terminal
- terminal is a popular CLI interface application in UNIX/LINUX based OSes
- different programming languages have different CLIs
- python has a default shell that is entered when python is called from the terminal app
- IPython is an interactive shell
- web browsers have a JavaScript shell in the console tab
GUI vs. terminal
- terminal uses text instead of graphics
- most things on the computer don’t need a graphical interface
- somethings are easier to do in the terminal
- terminal can be used to
- edit and run a program
- find files on the computer that have particular names
- download a file from the internet if the URL is known
- start a web-server on the computer
- start a web server on the cloud
- terminal cannot be used to
- edit a large motion picture
- mixing and mastering a music file
- perform graphical 3D modelling
- these operations need a GUI
advantages of CLI
- CLI interface is programmable
- efficient in terms of network bandwidth
- files on a remote system can be edited without having to be copied locally
- counter-intuitively, it is possible to be much more expressive on a CLI than over GUI
- remote servers usually do not have a GUI
- they are connected to and worked with through the CLI exclusively
virtual linux server
- vagrant along with VirtualBox offers Ubuntu virtualization
- VirutalBox is an application for running OSes on a virtual machine within a machine’s native OS
- Vagrant helps quickly and consistently configure such a virtual machine
- easy to manage multiple virtual machines in a virtual network within a single machine
- see VirtualBox’s website
- see Vagrant’s website
- get a virtual machine up and running using vagrant and an Ubuntu virtualbox to follow along
- login to the vm using
vagrant ssh
from the native terminal app
- login to the vm using
linux CLI
- in the native terminal app, if successfully logged in to the vagrant machine
- lands in the command prompt of the ubuntu terminal
- the terminal points to the login user’s home directory by default when it starts
- a login message is displayed by default
- usually with login time and IP address
- can be configured differently
- the rest of these notes are in that command prompt
terminal interface
- there is some variety in the way that terminal programs look
- but the contents don’t change much
- they can be configured differently on different systems, but usually displayed are:
- the login user name
- the host name/computer name
- the current time
- the current folder
- typically display:
- informational messages
- output of previously run commands
- prompt for entering new commands
- new commands are typed in the prompt and enter is pressed
- this is followed by actually running the command
- the result of running command is displayed as the output after completion of command processing
- after this, a fresh prompt is added to the bottom, ready for the next command
- each line in the terminal is either an input or an output
- input lines start with a command prompt - ends with a
$
- the output lines can vary, depending on the output produced by the program
- input lines start with a command prompt - ends with a
escape characters
- use a backslash before printing special characters like
'
and!
as they usually have reserved meanings in a shell’s context
terminal vs. shell
- terminal is an app that draws text on the screen and accepts input from the keyboard
- technically, it is an emulator of the old-school terminal hardware
- the terminal itself doesn’t know what to do with the input
- it needs an another app to handle that
- when things are typed into the terminal, it is sent to the shell
- when enter is pressed, the shell interprets the command it receives from the terminal
- then figures out what program to run,
- runs it and
- returns the output to the terminal
- the user can see the shell output displayed in the terminal
- the terminal can be used without the shell
- the default app in the terminal is the shell, other programs can be run in the terminal as well
- the python interpreter, for example, runs in the terminal when the shell calls it
- when python interpreter is exited, the linux shell is started in the terminal again (default behavior)
- there are multiple shells as well
- linux and macOS default to
bash
(GNU Bash) - others are:
ksh
: Korn shellcsh
: C shellzsh
: Z shellsh
: Bourne shell (default for BSD Unix)TCSH
: TENEX C Shell
- they all work slightly differently and have various different features
- linux and macOS default to
- the shell can be used without the terminal as well
- the shell commands can be arranged in a file
- it can then be fed directly to the shell
- this is called shell scripting
linux commands
- syntax:
<command-name> <agruments>
- the arguments follow the command name
- this is entered at the terminal command prompt
ls
- lists the files and folders in the current directory
- stands for long listing
- shows no output if there are no files in the current directory
- dynamically shaped output like this common
curl
- downloads files from the internet
- needs an address
- needs a file name to save the file as locally
curl http://udacity.github.io/ud595-shell/stuff.zip -o things.zip
- downloads files from the internet
date
- prints date and time in terminal
expr 2 + 2
- very simple calculator
- simple prints the expression “2 + 2” adds to 4
echo 'hello world'
- simply prints ‘hello world’
- like print statement in python
uname
- prints operating system name (“Linux”)
hostname
- prints name of the computer
- the virtual machine name if VM
host startpage.com
- prints info about webpage startpage.com
- includes IP address and mail handlers
bash --version
- prints version of linux shell
- some copyright info
python --version
- print version on python 2 installed
history
- prints the history of commands executed previously
uptime
- current time, time the computer’s uptime D-M-H, users logged in, load average
top
- displays processes running currently on the machine with the highest load at the top
- typing random things into the CLI
- usually throws an error if no command or program is recognized
command not found
- the shell prompt is returned after the error message
- ready to accept the next command
- usually throws an error if no command or program is recognized
comparison between a shell command and a python function
- similarities:
- they are both units of code
- both have a name and çan take arguments
rm weirdfile.txt
- deletes the file ‘weirdfile.txt’ in current directory
- similarly in the python shell,
os.remove("weirdfile.txt")
- deletes the file ‘weirdfile.txt’ in python current directory
- differences:
- they exist in different contexts
- most commands executed in the shell are whole programs
- they are separate OS threads/processes
- functions are used to organize scripts in languages rather than starting new processes