BASH/C2/Command-Line-arguments-and-Quoting/English
Title of script: Command line arguments and Quoting
Author: FOSSEE and spoken-tutorial team
Keywords: Video tutorial, Command Line Arguments, Single quote, Double quote, Backslash
|
|
---|---|
Display Slide | Welcome to the spoken tutorial on Command line arguments and Quoting in BASH |
Display Slide | In this tutorial, we will learn about
|
Display Slide
Prerequisites
|
To follow this tutorial, you should be familiar with the Linux Operating System.
|
Display Slide
System Requirements |
For this tutorial I am using
GNU Bash version 4 or above is recommended to practise this tutorial. |
Display slide :
Command Line arguments |
* Shell script accepts arguments from the command line.
|
Press Ctrl+Alt+T | Let us open the terminal by pressing Ctrl+Alt+T keys. |
On terminal>> Type gedit arg.sh&>> press Enter | I have already written the code in the file named arg.sh
gedit space arg.sh ampersand
|
Let me explain the code now. | |
#!/bin/bash | This is a shenbang or a bang line. |
[highlight]
echo "zeroth arg: $0" |
This line will print the Zeroth argument.
Here, $0 (Dollar zero) will print the name of the shell script. This in turn means that, the zeroth argument is the name of the program itself. |
[highlight]
echo "first arg: $1" |
$1 (Dollar one) represents the first argument passed to the program from the command line. |
[highlight]
echo "second arg: $2" |
Similarly,
$2 (Dollar two) represents the second argument passed to the program. |
[highlight] echo "third arg: $3" | $3 (Dollar three) represents the third argument. |
[highlight] echo "twelveth arg: ${12}" | And $12 (Dollar twelve) represents the twelveth argument. |
[highlight again!!]
echo "twelveth arg: ${12}"
|
To write an argument greater than 9, we need to use curly brackets.
|
echo "total args: $#" | To continue with our code -
$# (Dollar hash) gives the total number of arguments that have been passed to a program. |
echo "Args(dollar *): $*"
do echo “$arg” done |
$* (Dollar asterix) will print all the arguments on a single line.
|
echo "Args(dollar @): $@"
do echo “$arg” done |
$@ (Dollar at) will also print all the arguments. However, this time each argument will be on a seperate line.
|
exit 0 | exit 0 will exit the program. |
Now, save the file. | |
Switch to terminal
chmod +x arg.sh Press Enter |
And make it executable by typing,
|
Command line arguments are given during execution. | |
Highlight:
./arg.sh sunday monday tuesday
|
Now let us execute our Shell script by typing
dot slash arg.sh space sunday space monday space tuesday
|
Let me explain the output. | |
Output:
zeroth arg: ./arg.sh |
The zeroth argument always displays the command used to execute the program.
|
Output:
second arg: monday third arg: tuesday |
sunday is assigned to the first argument.
|
Output:
twelveth arg: |
Twelveth argument is empty.
|
Output:
total args: 3 |
We can see that total number of arguments i.e. 3, is assigned to $# (Dollar hash) |
Args(dollar *): sunday monday tuesday | As we already said earlier, $* (Dollar asterix) will print all the arguments as on a single line. |
sunday monday tuesday | This is the output of the first for loop.
|
Args(dollar @): sunday monday tuesday | These are the arguments printed by $@ (Dollar at) |
sunday
monday tuesday |
And this is the output of the second for loop.
|
<<PAUSE>>
Let's move on. Next, lets see Quoting is BASH. Switch to the slides. | |
Display slide
Quoting |
There are three types of quotes in BASH
|
Display Slide
Double Quote |
* Double quote substitutes the value of variables and commands
|
Switch to terminal>>Type echo “Username is $USER”>> press Enter | Let's switch to Terminal.
Type echo space opening double quote Username space is space dollar sign in capitals USER closing double quote
|
Output:
Highlight “Username is lavitha” |
This will display your username
<<PAUSE>> Let us switch to the slides. |
Display Slide
Single Quote |
* Single quotes preserves the literal meaning of each character of the given string.
|
Switch to terminal>>Type echo 'Username is $USER'>> press Enter | Switch to Terminal.
Type echo space open single quote Username space is space dollar sign in capitals USER close single quote and Press Enter |
Output
Username is $USER .
|
The output is Username is $USER
In this example, it prints all the characters which appear within the single quotes. It does not substitute the value of variable $USER
Switch back to the slides. |
Display Slide
|
* Backslash removes the special meaning from a single character
|
Switch to terminal>>Type echo Username is \$USER>> press Enter
|
Switch to Terminal.
Type echo space open double quote Username space is space backslash dollar sign in capitals USER close double quote
|
Output:
Highlight Username is $USER |
The output is Username is $USER
In this example the backslash removes the special meaning of (Dollar) $ symbol.
<<PAUSE>> |
This brings us to the end of this tutorial.
Let us switch back to slides and summarize. | |
Display slide
Summary |
In this tutorial we learnt,
|
Display Slide
|
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 |
Keep the same narration as Bash-1 and Bash-2 scripts for consistency.Reply to nancy (26/06/2013, 11:35): "..."
Resolved
|
The script has been contributed by FOSSEE and spoken-tutorial Team.
Thank you for joining. |