Linux and Bash Overview

Associate Professor Justin Dressel

Schmid College of Science and Technology

 

Bash

The "Bourne Again Shell"

  • "linux" = UNIX/MINIX clone written by Linus Torvalds
  • "shell" = interactive command line for manipulating files
  • "terminal" = text-based computer interface, runs a shell
  • "prompt" = output meaning "waiting for user input..."
  • "bash" = "Bourne Again Shell", standard in linux systems
    • (Alternatives: "csh" = C Shell, "zsh" = Z Shell)

Task:

  • Open a Terminal
    • Mac : in the Applications/Utilities folder
    • Linux : in the menu
    • Windows : install Git for Windows and use git-bash
  • The prompt "~$ " for the bash shell lets you know it is ready

A Bit of Linux Philosophy

Everything is a file.

(Directories, sockets, streams, web addresses, links, etc.

All are files, so obey the same basic interface.)

KISS : Keep It Simple Stupid.  

(Commands should do only what is asked,

and should report no output unless there is an error.)

Modularize design.

(Each command should do only one thing well.

Chain them together to do complex tasks.)

Linux Directory Tree

Files are organized into a tree structure in linux:

 

~$ tree -L 1 /
/
├── bin
├── boot
├── dev
├── etc
├── home
├── initrd.img -> boot/initrd.img-3.19.0-26-generic
├── initrd.img.old -> boot/initrd.img-3.19.0-25-generic
├── lib
├── lib32
├── lib64
├── lost+found
├── media
├── mnt
├── opt
├── proc
├── projects
├── projects2
├── root
├── run
├── sage
├── sbin
├── snapshots
├── srv
├── sys
├── tmp
├── tmp2
├── usr
├── var
├── vmlinuz -> boot/vmlinuz-3.19.0-26-generic
└── vmlinuz.old -> boot/vmlinuz-3.19.0-25-generic
 
26 directories, 4 files

Files are leaves.

Directories are branches.

The "root" directory is "/"

Paths to files are written as:

  • "/dir1/dir2/file"

Special paths:

  • "." = current working directory
  • ".." = parent directory
  • "~" = home directory

Getting Around

Useful bash commands:

  • "ls" = list files
  • "pwd" = print working directory
  • "cd" = change (working) directory
  • "mkdir" = make directory
  • "rmdir" = remove (empty) directory
  • "rm" = remove file
  • "mv" = move file
  • "cp" = copy file

Task:

  • Print your working directory - where are you in the file system?
  • List the contents of your directory - what is in there?

Useful control commands:

  • Up-arrow = review history
  • Ctrl-c = Stop running program
  • Ctrl-z = Pause running program
    • resume with "fg" or "bg"
  • Ctrl-w = Delete one word
  • Ctrl-u = Cut entire line
  • Ctrl-y = Paste a cut line
  • Ctrl-r = Search history

Options / Getting Help

Commands in bash may be given options:

  • Single-letter options, one dash:  "-A"
  • Many-letter options, two dashes:  "--a-whole-word"
~$ ls -l
    ...
~$ ls --all
    ...
~$ ls -la
    ...
~$ ls -lh
    ...

Examples:

Try these : what do they do?

(Note: not available in git-bash for Windows)

Command usually have a "manual page" to give you help:

~$ man ls

Task :

  • Run this command, understand the options above.  What other options for ls might you use in the future?
  • Tip: "man" uses the command "less" to format output. Navigate with arrows, or vim keys, exit with the letter 'q'.

Task :

  • What does this command do?
~$ tree -L 1 /

Permissions / Hidden Files

~/info$ ls -lah
total 157K
drwx------ 4 user user   11 Aug 29 06:57 .
drwx------ 5 user user    6 Aug 29 05:54 ..
drwx------ 8 user user   16 Aug 29 07:08 .git
-rw------- 1 user user 1.2K Aug 29 03:07 .gitignore
-rw------- 1 user user  551 Aug 29 06:56 .vimrc
-rw------- 1 user user  26K Aug 29 03:07 Chapman Coding Standards.pdf
-rw------- 1 user user  12K Aug 29 03:19 GettingStarted.md
-rw------- 1 user user 1.1K Aug 29 03:07 LICENSE
-rw------- 1 user user 6.7K Aug 29 03:07 README.md
drwx------ 4 user user    9 Aug 29 06:59 Templates
~/info$

~$ whoami
user

Permissions  -  Link #  -  User  -  Group  -  Size  -  Last Modified  -  Name

"ls -l" outputs:  (learn not to be intimidated by cryptic output!)

Permissions:  (as below, omissions indicated by a '-')

  • d rwx rwx rwx
  • (directory?)  (user: read? write? execute?)  (group: r? w? x?)  (other: r? w? x?)

Hidden Files:  Begin with period ".blah", revealed only with "-a"

Changing permissions: 

~$ chmod +x filename   # sets filename executable for user
~$ chmod u+w,g-w,o-rw filename # user +write, group -write, other -read&write

Variables and Control Flow

  • Variables are "dereferenced" by $ or ${ }
~$ thisisavariable="thisisastring"
~$ echo $thisisavariable
thisisastring
  • Commands can be run with $( )

(what does echo do?)

~$ dircontents=$(ls)
~$ echo $dircontents
2015-08-30-183918.term github randomwalk.sagews
  • Conditions follow: "if [ test ]; then cmd; else cmd2; fi" form
~$ t1="one"; t2="one"; if [ $t1 = $t2 ]; then echo "Match."; else echo "No match"; fi
Match.
~$ t1="one"; t2="two"; if [ $t1 = $t2 ]; then echo "Match."; else echo "No match"; fi
No match

(Note: the semicolons ; may be replaced with new lines for readability)

~$ if [ $t1 = $t2 ]; then
>     echo "Match."
> else
>     echo "No match."
> fi
No match.

For Loops and Pipes

  • Output can be "piped" into another command with |
~$ for w in this is a series of words; do echo $w.ext; done | less
  • For loops iterate over space/tab/new-line separated items
    They follow: "for var in list of items; do cmd; done" form
~$ for i in $(seq 9); do echo -n $i; done; echo
123456789

(what does less do?)

  • Output can be "directed" to a file with >
~$ for w in this is a series of words; do echo $w.ext; done > output.tmp
~$ cat output.tmp

(what does cat do?)

  • Output can be "appended" to a file with >>
~$ for i in $(seq 10000); do echo $i >> output.tmp; done
~$ cat output.tmp | grep 9 | head -n 10

(what do grep and head do?)

(what does seq do?)

~$ for user in /home/*; do echo ${user#/home/}; done
  • String variables can be modified as they are evaluated
        (This is particularly useful for tweaking filenames)

(Note: it is wise to use echo as above to make sure commands are correct first,

then press the up arrow to recall previous command, and remove echo second)

  • The * character is a "wildcard" that matches any pattern
  • The ? character is a "wildcard" that matches any one character
~$ for f in $(seq -s ".jpg " 10).jpg; do echo mv $f ${f/.jpg/.png}; done

Ex: ${var/str1/str2} replaces all instances of str1 in var with str2.

Ex: ${var#str1} removes one instance of str1 at start of var.

Ex: ${var%str1} removes one instance of str1 at end of var.

~$ for img in /home/*.jpg; do echo ${img%.jpg}.png; done

String Processing

Bash Scripting

All programs in linux have an "exit code", which is an integer value.

The success code is 0, which also means True in an if statement.

If a program exits with a nonzero value, it has failed (False). However, the specific number can give more information about exactly what failed. (See the man page for the program for what codes mean what.)

~$ bash -c "exit 0"
~$ echo $?
0
~$ bash -c "exit 1"
~$ echo $?
1

The "exit" command exits a script and returns the indicated exit code.

The special variable $? stores the exit code of the last run program.

The other special variables $1, $2, $3, etc. store the value of the first, second, and third command line argument, etc.

The variable $0 stores the name of the script itself.

#!/bin/bash

# This is a comment

echo "This is a line of code"

if [ $# == 0 ]; then
  echo "No command line arguments"
  exit 1 # This exits as a failure
fi

echo $1  # prints first argument

exit 0 # This exits as a success

Creating a Bash Script

Contents of file script.sh :

First line tells linux it is a bash script.

$# stores the number of script arguments

The final "exit 0" is usually omitted as implicit.

To make the script executable:

~$ chmod +x ./script.sh

To run the script directly:

~$ ./script.sh

Task: Try creating this script.

Further Reading

Practice makes perfect.

 

Keep references handy until you remember commands on command.

Linux and Bash Overview

By Justin Dressel

Linux and Bash Overview

These slides offer a condensed description of the essentials of using linux with the bash shell, for help in reaching proficiency quickly.

  • 4,883