Bash:Bash-introduction

From Juneday education
Jump to: navigation, search


Meta information about this chapter

Expand using link to the right to see the full content.

Books this chapter belongs to

Introduction

This chapter introduces the bash shell

Purpose

The purpose of this chapter...

Goal

The goal of this chapter...

Instructions to the teacher

Common problems

Chapter Videos

All videos in this chapter:

Svensk kanal på Vimeo: Introduktion till Bash: (Swe)


Description

Bash is a so called shell and acts as an interface between you and the operating system, using an interactive command line interface. A command line interface means that you write commands which are interpreted and executed by the shell when you press Enter.

An example would be to find out what directory you are working in (note that the dollar sign is not part of the command, it just signifies that bash is waiting for you to enter a command):

$ pwd
/home/ada
$

In this simple example, you entered the command pwd (which stands for print working directory) and bash answered /home/ada which is the "home directory" for the user named Ada. Note the empty line with a new dollar sign, showing that bash is ready for more commands.

When you start open terminal window (such as cygwin etc), bash is automatically loaded and started and typically your home directory is selected as the current working directory. When working in a command line environment, you are always "inside some directory". In later chapters, we will focus on creating directories and moving around between directories.

Typically, there is some text and then the dollar sign. In most cases the text before the dollar sign contains information on what user you are logged in as and on what computer and what directory you are inside (your current path). In examples here, we omit the path before the dollar sign, so that we can focus on the commands and results from issuing commands. The text with the current path followed by the dollar sign is called a "prompt". You can think of it as bash prompting you to enter the next command.

Exercises

Ex 01 - Examining the prompt

Open a terminal window (such as cygwin terminal if you are running cygwin on Windows). What does the terminal say? What is your current directory? (Look at the prompt)

Ex 02 - Running pwd

Issue the command pwd. What does bash answer you?

Ex 03 - Listing files and directory contents

It is useful to investigate what files are in the current directory. You can do this in many ways. Using bash you can use the command ls (which is short for list files). Type ls (and Enter). Do you see any files in your home directory? Is it really empty?

Ex 04 - Using file browser to look at directory contents

Locate current directory in your file explorer (on windows: explorer.exe). You can use the command explorer . (the dot is a nickname for the current directory) or just open explorer in some other way and navigate to the installation directory of cygwin and find the home directory in the hierarchy under cygwin.

Ex 05 - Using bc

Now it's time to use the calculator bc. (Note that if you get bc - command not found you need to install bc using the cygwin installation software, or similar if you are on a different platform - consult your teacher or supervisor) The bc command can be run interactively. An interactive program is a program which starts and waits for user input or actions. The interactive calculator bc can be started by issuing the command bc on the command line. The application then opens an interactive shell awaiting your commands. You may interactively give bc arithmetic expressions to evaluate and bc will answer immediately on the next line. To exit the program you type Ctrl-d which sends an "end-of-file" signal to the bc program, which then will exit and you'll end up back in bash again.

Start bc and use it to evaluate the following expressions:

  • 5*10
  • 10/3
  • 2^8
  • 2+3*4
  • 100/0

What results did bc give you? Did you expect those results? What do you think is the reason for the result of the divisions?

Exit bc by typing Ctrl-d. You are now back in bash.

Start bc giving it the flag -l (minus lower case L):

$ bc -l
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.

Issue the following expression again: 10/3

What was the result this time?

Enter the following, still in interactive mode scale=2 and on the next line, repeat the expression 10/3. What was the result this time? What do you think scale=2 means?

Ex 06 - Making bash talk

Now let's look at a simple command for making bash output text in the terminal window, echo. Let's make bash write some text to the terminal using the echo command.

Issue the following command:

$ echo "Hello bash"

What was output to the screen? Was it only text? (this is a tricky question)

Now let's use a flag -n:

$ echo -n "Hello bash"

What was output to the screen this time? Was it only text? (Hint: the flag -n means "do not output the trailing newline")

Now, let's test another flag, -e (stands for "enable interpretation of backslash escapes"):

$ echo -e "Hello\nbash"

What was output this time?

Solutions

Expand using link to the right to see the full content.

Solution to 1

Your results depend on what your user is called on your system. If your user is called Ada the prompt would be something like:

GNU/Linux

Ada@computername:~$

cygwin

Ada@computername ~
$

(In Cygwin, the default prompt includes a newline before the $

MacOS

computername:~ Ada$

Solution to 2

Depending on what your user is called, the answer would be different, but again, assuming your user is called Ada (for instance) the answer would be something like:

/home/Ada

Solution to 3

We can't really know if you have any files in your home directory, but if you do, then those files will be listed. If you have just installed cygwin (assuming you are not running GNU/Linux or MacOS), then the home directory will look empty. But it is not true that it is empty! There are some "hidded" files, which is files whose names start with a dot. To see these using ls, you must add an option (or flag) to ls like this:

$ ls -a

The flag -a means "all files" (including hidden files).

Solution to 4

Just do it or ask a class mate or teacher if you cannot figure out how to do this.

Solution to 5

The results would be as shown in the following session:

$ bc
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'. 
5*10
50
10/3
3
2^8
256
100/0
Runtime error (func=(main), adr=7): Divide by zero

What might surprise you is that the division 10/3 is evaluated to 3. The default mode for division in bc started without any flags is to use integer division. The division by zero error message is just bc's way of telling you that division by zero is impossible to resolve.

However, when you run bc with the -l flag (minus lower case L), it switches to real division. The result is shown below:

$ bc -l
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'. 
10/3
3.33333333333333333333
scale=2
10/3
3.33

The command scale=2 inside an interactive session with bc sets the number of decimals to 2. It doesn't round the result.

Solution to 06

Here's a copy of a session issuing all the commands from ex 06:

$ echo "Hello bash"
Hello bash

Ok, "Hello bash" is printed out. But the prompt begins at the next line. What's going on here? In the age of typewriters there was a special key that switched the paper to the next line. Some thing with terminal (and text files actually). If you want the text to stop at a specific place and continue on the next line you need to insert a newline.


$ echo -n "Hello bash"
Hello bash$

(For MacOS (where the prompt doesn't contain a newline)

On cygwin, the result is a little different:

user@computer:~

$ echo -n "Hello bash"
user@computer:~
$

We can see that the prompt ($) is printed directly after the "Hello bash" text (in MacOS - In cygwin the extra blank line is gone). In the previous printout the prompt was printed on a new line after the text. So with the argument -n we tell echo to NOT add a newline automatically.

$ echo -e "Hello\nbash"
Hello
bash

In this last example we see that a newline has been added after "Hello". This newline is caused by the "\n" character in the string "Hello\nbash".

Videos

  • Solutions to all exercises: (eng)

Links

External links

Where to go next

The next chapter is about Files and Folders.

« PreviousBook TOCNext »