Class website reminder

Today’s objectives

Topic 2: Intro to Bash

  • Most people communicate with their computer through graphical user interfaces (GUI)
  • These GUIs are translators
    • Translates your clicks into instructions
  • Works well for simple tasks, but does not scale well
    • E.g., what if you need to gather info from 1000 files?
    • Click, click, click …
  • If you vacation in a foreign country, a translator is very helpful
  • But, what if you move to that country for a job in which you interact with hundreds of people every day?
  • A translator only gets you so far
  • We want to learn the language used by the GUI to talk (more) directly to our computer

About these slides

I will use a “Bash command line” heading for lines we type at the command prompt

Bash command line
echo "Hello World!"

I will use a “Bash script” heading for lines we type in a file

Bash script
echo "Hello World!"

Anatomy of a command

Bash command line
echo -e "Hello, World!"
echo
Command (program)
-e
Option or “flag”
"Hello, World!"
Argument

Precision (spelling, case, and spaces) matters

Bash command line
echo - e "hello"
Echo "hello"
eco "hello"

Getting help with a command

Bash command line
man echo
  • This shows the manual for the echo command
    • Navigate with arrow keys (or ‘j’/‘k’)
    • Press ‘q’ to exit the manual
  • Note, man is the command (program) and echo is an argument

File System & Paths

Bash command line
pwd
ls /
ls /arc
ls /arc/homes/jro0014
ls

pwd shows the path to your current location in the file system (print working directory)

ls lists files and directories

Let’s make 2 new directories move into/out of them

Bash command line
mkdir foo bar
cd foo
pwd
cd ..
pwd
cd /arc/homes/jro0014/foo
pwd
cd ..


Bash command line
cd foo
pwd
cd ../bar # same as cd /arc/homes/jro0014/bar

How to get home?

  • There are several ways
Bash command line
cd
pwd
cd ~
pwd
cd $HOME
pwd

$HOME and ~ are just variables for your home directory

Bash command line
echo $HOME
echo ~

You can use these variables for relative paths

Bash command line
cd foo
cd ~/bar # same as ../bar or /arc/homes/jro0014/bar

What are commands?

  • They are just programs!
    • Files on your computer that can be executed

whereis is a command for finding files in your PATH

Bash command line
whereis -b pwd
pwd
/bin/pwd
whereis -b ls
ls
/bin/ls
whereis -b whereis
  • The reason you don’t have to type /bin/pwd is because /bin is in your PATH
Bash command line
echo $PATH
  • When you type pwd, Bash will look for it in these directories

For loop

Bash command line
for x in 1 2 3 4; do echo hello; done


Bash command line
for x in 1 2 3 4; do echo $x; done

Let’s make a file with one word in it

Bash command line
echo how > file1.txt
  • “>” redirects the output of echo into the file “file1.txt”

Confirm the file1.txt exists and has “how” in it

Bash command line
ls
cat file1.txt

Let’s make 2 more files

Bash command line
echo are > file2.txt
echo you? > file3.txt

Let’s loop over the files and print their content

Bash command line
for f in file1.txt file2.txt file3.txt; do cat $f; done

We can use wildcards to make this easier

Bash command line
for f in file?.txt; do cat $f; done
  • “?” will match any character at that position


Bash command line
for f in f*.txt; do cat $f; done
  • “f*.txt” will match any file that starts with “f” and ends with “.txt”

You are coding!

Let’s make a file and open it with a text editor

(I use VS Code below, but you can use any text editor)

Bash command line
touch my_bash_script.sh
code my_bash_script.sh

VS Code might open an authentication prompt; use your AU password

Add these lines to my_bash_script.sh

Bash script
echo "Hello, World!"

for x in 1 2 3 4; do echo $x; done

Save and close the my_bash_script.sh file

Now, you can run my_bash_script.sh as a script with Bash!

Bash command line
bash my_bash_script.sh

How to do this on your computer?

Not required! But, if you want to use a Unix-like command line on your computer, you can!

macOS users

You have a Terminal app already

  • Open Spotlight, type “Terminal”, and press Return
  • The default shell is Zsh (not Bash), but it is very similar

I recommend installing Xcode command line tools

  • Open terminal and use this command:

    Zsh command line
    xcode-select --install

Windows users

You can install Linux via the Windows Subsystem for Linux (WSL)

I recommend you use the default Linux distribution (Ubuntu)

Running WSL will be very similar to Red Hat (ThinLinc)

Resources

Here’s a nice lesson from Software Carpentries

http://swcarpentry.github.io/shell-novice/

Acknowledgments

Support

This work was made possible by funding provided to Jamie Oaks from the National Science Foundation (DEB 1656004).