Review of Bash and Zsh

Notes to review Bash and Zsh programming

Background

Shell is interface to run programs via commands. It is text based (command line). The Bash and Zsh (default on MacOS) are most popular. Windows has Powershell but lately can support Bash/Zsh as well via (WSL – Windows Subsystem for Linux). Zsh is very similar to Bash but more configurations/features. Use cases

  • File Management
  • System Admistration
  • Software Development
  • Data Filtering / Transform / Analysis
  • Remote Access
  • Programming (scripting)

Common Commands and Tools

  • Nano = text editor, small easy to use, shows commands on screen
  • File command – gives file info
  • open command – opens file with default editor defined by OS
  • cat = best for small files, no paging, may mess up terminal
  • less = pager with lots of features
  • vi = text editor, fast, powerful, requires knowing key commands
  • rm = remove, destructive careful, no recycle bin/ permenant
  • touch = creates files, can space delimit creating multiple files. Use ‘ characters to create files with space in the names

File names are case sensitive. Avoid these characters:

  • Quotes ` ‘ “
  • Brackets/Parans {} () [] <>
  • Interpunctions ! ? & |
  • Other $ @ ~
  • Whitespace/Tabs

Pipes

Using the | pipe

Takes the output from left and feeds it as input for command on the right

  • ls -alrt | grep hello

Wildcards

Using the * wildcard

Use to substitute characters with wildcard

  • grep tiny *
  • mv *txt text

Superuser

Privileged root user

Can be used with sudo command. Stands for SuperuserDo

export

Set environment variables

  • export EDITOR=nano

.zshenv

Environment variables file for the whole shell

$PATH

Environment variable containing all dependency paths.

Sample Bash Script

#! /usr/bin/bash
# declare HELLO variable
HELLO="Hello World"
# print variable on a screen
echo $HELLO
# remember to chmod the file
# chmod +x hello_world.sh
  • Line 1 line of any script should be the location of the interpreter for the script. When a script is executed, this location is used to find the program that will read the script and interpret it; hence, an “interpreter”. To indicate that the first line is the path to the interpreter, the “shebang” is used, which is the combination of ‘#!’ (hashe-bang).
  • Lines 2 and 4 are comments, designated by the # symbol at the beginning.
  • Line 3 is the declaration of our variable, HELLO. Keep in mind that there is no type declaration as there would be in C or Java indicating what type of variable we are creating. We simply choose a name for our variable and assign it a value. The Bash interpreter knows that HELLO is a string because it is assigned to a value that is a string.
  • Line 5 uses the echo command to print something to the screen. echo is simply a program that can be used at any time in the shell. Try this now by typing “echo stuff” into your terminal to see the results. The $ symbol prior to the HELLO variable is a symbol used to reference our variable. If we left off this symbol, the output of our program would be “HELLO” instead of “Hello World”.

 

Positional Parameters

#!/usr/bin/bash
echo $1 $2 $3

 

Loops

#!/usr/bin/bash
# for loop
for f in $( ls /var/ ); do
        echo $f
done

COUNT=6
# while loop
while [ $COUNT -gt 0 ]; do
        echo Count: $COUNT
        let COUNT=COUNT-1
done

#cache
#lib
#local
#lock
#log
#mail
#opt
#run
#spool
#tmp
#Count: 6
#Count: 5
#Count: 4
#Count: 3
#Count: 2
#Count: 1

 

String Manipulation

#!/usr/bin/bash
foo=”Hello”
foo=”$foo World”
echo $foo

a=”hello”
b=”world”
c=$a$b
echo $c

echo ${#foo} # number of characters 11

 

String Manipulation

#!/usr/bin/bash
foo=”Hello”
foo=”$foo World”

 

 

 

References

Getting Started with Bash and Zsh
https://app.pluralsight.com/library/courses/bash-zshell-getting-started/table-of-contents

Getting Started with Shell Scripting
https://app.pluralsight.com/library/courses/shell-scripting-bash-zshell-getting-started/table-of-contents

 

 

eof