BASH/C2/Globbing-and-Export-statement/English

From Script | Spoken-Tutorial
Jump to: navigation, search

Title of script: Globbing and Export Statement

Author: FOSSEE and spoken-tutorial team

Keywords: Video tutorial, Globbing, Export statement


Visual Cue
Narration
Display Slide Welcome to this spoken tutorial on Globbing and Export command
Display Slide In this tutorial, we will learn about
  • Globbing
  • export command


Display Slide

Prerequisites


spoken-tutorial.org

To follow this tutorial,

You should be familiar with Linux Operating System


If not, for relevant tutorials please visit spoken hyphen tutorial dot org

Display Slide

System Requirements

For this tutorial I am using
  • Ubuntu Linux 12.04 OS and
  • GNU Bash version 4.1.10

GNU Bash version 4 or above is recommended to practise this tutorial.

Display Slide

Globbing

Let us start with an introduction to globbing.
  • Filename or pathname expansion carried by BASH is known as Globbing.
  • Globbing recognizes and expands wildcards.
  • It also interprets standard wildcard characters like
  1. * (asterix) and
  2. ? (Question mark)


Let us understand this with the help of an example.
Press Ctrl+Alt+T Open the terminal by pressing Ctrl+Alt+T keys.
On Terminal>> Type ls *.sh>> Press Enter On the Terminal type,

ls space asterix dot sh

and press Enter.

[Highlight]

*.sh

It matches all the files having .sh extension in the present directory.

<<PAUSE>>

Highlight:

Output:


array.sh

Array.sh

comm.sh

data.sh

if.sh

Here we can see that all the sh files are listed.
On Terminal>> Type ls c*.sh>> Press Enter Next, on the Terminal type,

ls space c asterix dot sh

and press Enter.

[Highlight]

c*.sh

It will match all the files starting with character 'c' & having sh extension.

<<PAUSE>>

Output:

Highlight:

comm.sh

We can see that all files starting with character 'c' and extension sh.
On Terminal>> Type ls [a-c]*.sh>> Press Enter Now on the Terminal, type,

ls space opening square bracket a, hyphen, c closing square bracket asterix dot sh

and press Enter.

Highlight:

[a-c]*

This will match and display the files starting with letter a or b or c.

<<PAUSE>>

Output:

Highlight:

array.sh

comm.sh

Observe the output.

We see a list of all files starting with character a or b or c.

And these files have sh extension.

On Terminal>> Type ls [^a-c]* .sh>> Press Enter Next, let's go ahead and type

ls space opening square bracket caret symbol a hyphen c closing square bracket asterix dot sh

and press Enter.

Highlight:

[^a-c]*.sh

This will match all the filenames with extension sh.

But will omit those with starting character as 'a' OR 'b' OR 'c'

<<PAUSE>>

Output:

Highlight:


data.sh

if .sh

Obsere the output.

You will notice that the filenames are not starting with character 'a' or 'b' or 'c'.

And the extension is sh .

On Terminal>> Type ls [Aa]*.sh>> Press Enter Let's go ahead.

On the Terminal type,

ls space opening square bracket capital 'A' and lower case 'a' closing square bracket asterix dot sh

and press Enter.

Highlight:

[Aa]*

This will match filenames starting with

upper and lower case of letter 'A'.

Output:

Highlight:

array.sh

Array.sh

Let's see the output.

All filenames starting with upper and lower case ''A'' and extension sh are listed.

<<PAUSE>>

Now let's see the Export command in BASH.


Switch to the slides.

Display Slide

Export

* I have made some change in the language.
Pls check if this is okay.In Bash, variables are local to their own Shell.
  • Local variables can be used by the same Shell or by the current Shell.


Display Slide

Export

I have made some change in the language.

Pls check if this is okay.The Export command

  • Exports a variable or a function to the environment of all child processes.
  • Can also change a local variable to a global variable.


We will try to understand this with an example.
Switch to Terminal>>

myvar=lion>> Press Enter

echo $myvar>> Press Enter


Switch to the Terminal and type,

myvar equal to lion

and press Enter.


Now type,

echo space dollar symbol myvar

and press Enter.

Output:

lion

lion is printed.



Highlight myvar=lion This was the value assigned to the variable myvar.
On Terminal>>

Type

/bin/bash>> Press Enter


Now, let's navigate to a new Shell.


To go to a new Shell, we can either open a new Terminal or type,

slash bin slash bash

and press Enter.

Type

echo $myvar>> Press Enter

Now let's check the value in the variable myvar.

Type,

echo space dollar symbol myvar

and press Enter.

[Highlight]

Output:


An empty line is printed.

This means that the value assigned to variable myvar was not transferred to this Shell.

Also, the variable myvar is local only to the previous Shell and not to current Shell.

Type exit We will type exit to get back to our previous Shell.

<<PAUSE>>

[Highlight] So, to declare variables globally, we have to use the export command.
On Terminal>> Type export myvar=lion>> Press Enter>>


Let's learn how.

On the Terminal type,

export space myvar equal to lion

and press Enter.

Type

echo $myvar>> Press Enter

Now type,

echo space dollar symbol myvar

and press Enter.

Output: lion lion is displayed.
On Terminal>>Type /bin/bash>> Press Enter Now, let's navigate to another Shell by typing, slash bin slash bash

and press Enter.

Type

echo $myvar>> Press Enter

Now type, echo space dollar symbol myvar and press Enter.
Output: lion lion is displayed

This is because we have declared the variable myvar globally.

<PAUSE>

This brings us to the end of this tutorial.

Let us summarize. Come back to our slides.

Display slide 10

Summary

In this tutorial we learnt,
  • Globbing
  • Export command


Display slide 11

Assignment

Write a Bash script
  1. to do all the operations discussed under globbing in this tutorial.


Display Slide 12 Watch the video available at the link shown below

It summarises the Spoken Tutorial project

If you do not have good bandwidth, you can download and watch it

Display Slide

Spoken Tutorial Workshops

The Spoken Tutorial Project Team

Conducts workshops using spoken tutorials

Gives certificates to those who pass an online test

For more details, please write to

contact@spoken-tutorial.org

Display Slide

Acknowledgement

Spoken Tutorial Project is a part of the Talk to a Teacher project

It is supported by the National Mission on Education through ICT, MHRD, Government of India

More information on this Mission is available at: http://spoken-tutorial.org\NMEICT-Intro

The script has been contributed by FOSSEE and Spoken-Tutorial team.


This is Ashwini from IIT Bombay.

Thank you for joining.



Contributors and Content Editors

Ashwini