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 for practise. |
Display slide :
Command Line arguments |
* Shell script can accept arguments from the command line.
|
Press Ctrl+Alt+T | Let us open the terminal by pressing Ctrl+Alt+T keys simultaneously on your keyboard. |
On terminal>> Type gedit arg.sh&>> press Enter | I have already written the code in the file named arg.sh
gedit space arg.sh space ampersand sign
Now, Press Enter. |
The text editor is opened.
Let me explain the code now. | |
#!/bin/bash | This is the 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. |
Let us execute the program and see.
Switch to the terminal. First make it executable by typing, chmod space plus x space arg.sh and press Enter Now type, dot slash arg.sh | |
The output is displayed as:
Zeroth argument is arg.sh | |
Now come back to our editor and type the three lines as shown here. | |
[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. |
Now click on Save,
Let us execute the program and see. Press the uparrow key and press Enter | |
We see that the first argument is printed.
But the first, second and third argument are blank. This is because the Command line arguments are given during execution. | |
Hence press the uparrow key and type:
sunday monday and tuesday. Press Enter You can see that the first second and third arguments are: sunday monday and tuesday | |
Now switch back to our editor press Enter
Now type the line as shown here. | |
[highlight] echo "twelveth arg: ${12}" | $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.
|
Now click on Save.
Let us execute the program. Switch to the temrinal. Let me clear the prompt. | |
Now we need to give 12 or 13 arguments to the program. | |
Hence Type, dot slash arg.sh 1 to 13.
Now press Enter | |
You can see that the 12th argument is 12.
Come back to our editor. And type the line as shown here. | |
echo "total args: $#" | $# (Dollar hash) gives the total number of arguments that have been passed to a program. |
Now click on Save.
Let us execute. Switch to the temrinal. | |
Let us execute.
Press the uparrow key and press Enter. We can see that the total arguments are 13. Now switch to the editor. Press Enter and type the lines as shown here. | |
echo "Args(dollar *): $*"
do echo “$arg” done |
$* (Dollar asterix) will print all the arguments on a single line.
|
Now click on Save.
Switch to the temrinal. Let me clear the prompt. Now let us type, dot slash arg.sh space sunday monday and tuesday Press Enter | |
You can see that the total number of arguments are 3.
As we have passed 3 arguments to our program. As already said $* will print all the arguments on a single line. | |
And this is the output for the for loop.
We see that all the arguments on a single line. Now move back to our program and type the lines as shown here. | |
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.
|
Let us see how. Click on Save
Switch to the temrinal. Press the uparrow key. Press Enter You can see the difference now. | |
These are the arguements printed by $@.
$@ prints each argument on seperate line. This is the output for the 2nd for loop. | |
Now lets move on to quoting in BASH
Switch to the slides. | |
Display slide
Quoting |
There are three types of quotes
|
Display Slide
Double Quote |
* Double quote substitutes the value of variables and commands
|
Switch to terminal>>Type echo “Username is $USER”>> press Enter | Switch to Terminal.
Type echo space within double quotes Username space is space dollar sign in capitals USER
|
Output:
Highlight “Username is lavitha” |
The username of the system is printed.
The output wil vary according to your system. <<PAUSE>> Now move back to 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 the Terminal.
Type echo space within single quote Username space is space dollar sign in capitals USER 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 our slides. |
Display Slide
|
* Backslash removes the special meaning from a single character
|
Switch to terminal>>Type echo Username is \$USER>> press Enter
|
Switch to the Terminal.
Type echo space within double quote Username space is space backslash dollar sign in capitals USER
|
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. Let us 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 |
The script has been contributed by FOSSEE and spoken-tutorial Team.
Thank you for joining. |