Difference between revisions of "BASH/C2/Command-Line-arguments-and-Quoting/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with ''''Title of script''':''' Command line arguments and Quoting''' '''Author: '''FOSSEE and spoken-tutorial team '''Keywords: '''Video tutorial,''' '''Command Line Arguments, Sing…')
 
 
(2 intermediate revisions by 2 users not shown)
Line 19: Line 19:
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| In this tutorial, we will learn about
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| In this tutorial, we will learn about
  
* '''Command line Arguments'''
+
* '''Command line Arguments '''and
 
* '''Quoting'''
 
* '''Quoting'''
 
 
  
 
|-
 
|-
Line 45: Line 43:
 
* '''GNU Bash''' version '''4.1.10'''
 
* '''GNU Bash''' version '''4.1.10'''
  
'''GNU Bash '''version '''4''' or above is recommended to practise this tutorial.
+
'''GNU Bash '''version '''4''' or above is recommended for practise.
  
 
|-
 
|-
Line 51: Line 49:
  
 
'''Command Line arguments'''
 
'''Command Line arguments'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| * '''Shell script''' accepts '''arguments''' from the '''command line'''.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"|  
* The '''arguments''' are passed to the '''program''' being called.
+
* '''Shell script''' can accept '''arguments''' from the '''command line'''.
 +
* An '''argument''' is passed to the '''program''' being called.
 
* Any number of '''arguments''' can be passed to a '''program'''.
 
* Any number of '''arguments''' can be passed to a '''program'''.
 
 
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Press '''Ctrl+Alt+T'''
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Press '''Ctrl+Alt+T'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us open the '''terminal''' by pressing '''Ctrl+Alt+T''' keys.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us open the '''terminal''' by pressing '''Ctrl+Alt+T''' keys simultaneously on your keyboard.
  
 
|-
 
|-
Line 68: Line 65:
 
On the '''terminal, l'''et me open this file by typing,  
 
On the '''terminal, l'''et me open this file by typing,  
  
'''gedit''' '''space arg.sh ampersand'''
+
'''gedit''' '''space arg.sh space ampersand sign'''
  
  
Here '''ampersand '''is used to free up the '''prompt'''.
+
We use the '''ampersand to''' free up the '''prompt'''.
  
 +
Now,
  
 
Press''' Enter.'''
 
Press''' Enter.'''
Line 78: Line 76:
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let me explain the code now.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The text editor is opened.
 +
 
 +
Let me explain the code now.
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''<nowiki>#!/bin/bash </nowiki>'''
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''<nowiki>#!/bin/bash </nowiki>'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This is a '''shenbang '''or a '''bang''' line.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This is the '''shenbang '''or a '''bang''' line.
  
 
|-
 
|-
Line 93: Line 93:
  
 
This in turn means that, the '''zero<sup>th</sup> argument''' is the name of the '''program''' itself.
 
This in turn means that, the '''zero<sup>th</sup> argument''' is the name of the '''program''' itself.
 +
 +
|-
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Switch to the terminal.
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us execute the program and see.
 +
 +
Switch to the terminal.
 +
 +
|-
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Type chmod plus x arg.sh
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| First make it executable by typing,
 +
 +
'''chmod space plus x space arg.sh'''
 +
 +
and press '''Enter'''
 +
 +
|-
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Type
 +
 +
'''./arg.sh'''
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now type,
 +
 +
'''dot slash arg.sh'''
 +
 +
|-
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The output is displayed as:
 +
 +
'''Zeroth argument is arg.sh'''
 +
 +
|-
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now come back to our editor and type the three lines as shown here.
  
 
|-
 
|-
Line 104: Line 136:
  
 
'''echo "second arg: $2"'''
 
'''echo "second arg: $2"'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Similarly,
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"|  
  
 
'''$2''' '''(Dollar two) '''represents the second '''argument''' passed to the '''program'''.
 
'''$2''' '''(Dollar two) '''represents the second '''argument''' passed to the '''program'''.
Line 113: Line 145:
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| <nowiki>[highlight] </nowiki>'''echo "twelveth arg: ${12}" '''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Click on Save.
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| And '''$12 (Dollar twelve) '''represents the''' twelveth '''argument.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now click on '''Save''',
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| <nowiki>[highlight again!!]</nowiki>
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Press the uparrow key>> press '''Enter'''
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us execute the program and see.
  
'''echo "twelveth arg: ${12}"'''
+
Press the uparrow key and press '''Enter'''
  
 +
|-
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We see that the zeroeth '''argument''' is printed.
  
 +
But the first, second and third '''arguments''' are blank.
  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To write an '''argument''' greater than 9, we need to use '''curly brackets'''.
+
This is because the '''command line arguments '''are given during execution.
  
 +
|-
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Press the uparrow key and type: '''sunday monday and tuesday.'''
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Hence press the uparrow key and type:
  
Else '''bash''' will only take the '''argument''' of the integer in the '''ten's place'''.
+
'''sunday monday and tuesday.'''
  
 +
Press '''Enter'''
  
And you will not get expected output.
+
|-
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| You can see that the first second and third '''arguments''' are:
 +
 
 +
'''sunday monday and tuesday'''
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''echo "total args: $#" '''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To continue with our code -
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now switch back to our editor. Press '''Enter'''
  
'''$# (Dollar hash)''' gives the total number of '''arguments''' that have been passed to a program'''.'''
+
Now type the line as shown here.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''echo "Args(dollar *): $*"'''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| <nowiki>[highlight] </nowiki>'''echo "twelveth arg: ${12}" '''
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''$12 (Dollar twelve) '''represents the''' twelveth '''argument.
  
 +
|-
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| <nowiki>[highlight again!!]</nowiki>
  
'''for arg in “$*”'''
+
'''echo "twelveth arg: ${12}"'''
  
'''do'''
 
  
'''echo “$arg”'''
 
  
'''done'''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To write an '''argument''' greater than 9, we need to use '''curly brackets'''.
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''$* (Dollar asterix)''' will print all the '''arguments''' on a '''''single line'''. ''
+
  
  
We will test this with the help of a simple''' for '''loop.
+
Else '''bash''' will only take the '''argument''' of the integer in the '''ten's place'''.  
  
  
We will analyse this '''for '''loop at the time of execution.
+
And you will not get expected output.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''echo "Args(dollar @): $@" '''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Click on Save.
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now click on '''Save.'''
  
 +
|-
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Switch to the '''terminal.'''
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"|Let us execute the program.
  
'''for arg in “$@”'''
+
Switch to the '''terminal'''.
  
'''do'''
+
|-
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Type '''clear'''
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"|Let me clear the '''prompt'''.
  
'''echo “$arg”'''
+
|-
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now we need to give 12 or 13 '''arguments''' to the program.
  
'''done'''
+
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''$@ (Dollar at)''' will also print all the '''arguments'''. However, this time each '''argument''' will be on a '''''seperate line'''.''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Type, '''dot slash arg.sh''' '''1 to 13.'''
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Hence type, '''dot slash arg.sh''' '''1 to 13.'''
  
 
+
Now press''' Enter'''
This is another '''for''' loop, which will print each '''argument''' in a seperate line.
+
 
+
|-
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''exit 0'''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''exit''' '''0 '''will exit the program.
+
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now, save the file.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| You can see that the 12th''' argument''' is '''12.'''
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Switch to '''terminal'''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Go back to the editor.
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Come back to our editor.
  
 +
And type the line as shown here.
  
Type:
+
|-
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''echo "total args: $#" '''
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''$# (Dollar hash)''' gives the total number of '''arguments''' that have been passed to a program.
  
'''chmod +x arg.sh'''
+
|-
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now click on '''Save.'''
  
Press''' Enter'''
+
Let us execute.
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| And make it executable by typing,
+
  
 +
|-
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Switch to the terminal.
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Switch to the terminal.
  
'''chmod''' '''space plus x space arg.sh'''
+
|-
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us execute.
  
 
+
Press the uparrow key and press '''Enter.'''
and press '''Enter.'''
+
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''Command line arguments '''are given during execution.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"|We can see that the total '''arguments''' are '''13.'''
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''Highlight:'''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|Switch to the editor.
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"|Now switch to the editor.
  
 +
Press '''Enter''' and '''type''' the lines as shown here.
  
Type:
+
|-
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''echo "Args(dollar *): $*"'''
  
'''./arg.sh sunday monday tuesday'''
 
  
 +
'''for arg in “$*”'''
  
Press '''Enter'''
+
'''do'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now let us execute our '''Shell script''' by typing
+
  
'''dot slash arg.sh space sunday space monday space tuesday'''
+
'''echo “$arg”'''
  
 +
'''done'''
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''$* (Dollar asterix)''' will print all the '''arguments''' on a '''''single line'''. ''
  
Here '''sunday, monday '''and '''tuesday '''are the '''arguments''' we are passing.
 
  
 +
|-
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight the for loop.
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We will test this with the help of a simple''' for '''loop.
  
Now press '''Enter.'''
+
 
 +
We will analyse this '''for '''loop at the time of execution.
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let me explain the output.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now click on '''Save.'''
 +
 
 +
Switch to the terminal.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''Output:'''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Type clear.
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"|
  
'''zeroth arg: ./arg.sh '''
+
Let me clear the prompt.
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The '''zeroth''' '''argument''' always displays the '''command '''used to execute the program.
+
  
 +
|-
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Type, '''./arg.sh sunday monday and tuesday'''
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now let us type, '''dot slash arg.sh space sunday monday and tuesday'''
  
So, you will see '''./arg.sh '''here.
+
Press''' Enter'''
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Output:
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| You can see that the total number of '''arguments''' are '''3''' as we have passed 3 '''arguments''' to our program.
  
 +
|-
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"|As already said '''$*''' will print all the '''arguments''' on a single line.
  
'''first arg: sunday'''
+
|-
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| And this is the output for the '''for loop.'''
  
'''second arg: monday '''
+
We see that''' '''all the '''arguments''' on a '''''single line'''.''
 
+
'''third arg: tuesday'''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''sunday''' is assigned to the '''first argument'''.
+
 
+
 
+
'''monday '''is assigned to the '''second argument.'''
+
  
 
+
|-
and '''tuesday '''is assigned to the '''third argument.'''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now move back to our program and '''type''' the lines as shown here.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Output:
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''echo "Args(dollar @): $@" '''
  
'''twelveth arg: '''
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''Twelveth argument''' is empty.
 
  
 +
'''for arg in “$@”'''
  
This is because we have passed only '''three arguments''' to the program.
+
'''do'''
  
 +
'''echo “$arg”'''
  
Recall, this was the '''argument''' with '''curly brackets'''.
+
'''done'''
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''$@ (Dollar at)''' will also print all the '''arguments'''.  
  
|-
+
However, this time each '''argument''' will be on a '''''separate line'''.''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''Output:'''
+
  
'''total args: 3 '''
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We can see that total number of '''arguments''' i.e. '''3,''' is assigned to '''$# (Dollar hash)'''
 
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''Args(dollar *): sunday monday tuesday'''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight the other for loop
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| As we already said earlier, '''$* '''(D'''ollar asterix) '''will print all the '''arguments''' as on a single line.  
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This is another '''for''' loop, which will print each '''argument''' in a separate line.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''sunday monday tuesday'''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This is the output of the first '''for '''loop.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us see how. Click on '''Save'''
  
 
+
Switch to the terminal.
We see that all '''arguments''' are printed on a single line.
+
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''Args(dollar @): sunday monday tuesday '''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| These are the '''arguments''' printed by '''$@ (Dollar at)'''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"|Press the''' uparrow key.'''
  
|-
+
Press''' Enter'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''sunday'''
+
  
'''monday'''
+
You can see the difference now.
  
'''tuesday'''
+
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| And this is the output of the second '''for''' loop.  
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| These are the '''arguments''' printed by '''$@.'''
  
 +
'''$@''' prints each '''argument''' on separate line.
  
'''$@ (Dollar at)''' prints each '''argument''' on a seperate line.
+
This is the output for the 2nd '''for loop.'''
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| <nowiki><<PAUSE>></nowiki>
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now lets move on to '''quoting''' in '''BASH'''
 
+
Let's move on.
+
 
+
Next, lets see '''Quoting '''is '''BASH.'''
+
  
 
Switch to the slides.
 
Switch to the slides.
Line 309: Line 374:
  
 
'''Quoting'''
 
'''Quoting'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| There are three types of quotes in '''BASH'''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| There are three types of quotes  
  
 
# '''Double quote'''
 
# '''Double quote'''
 
# '''Single quote'''
 
# '''Single quote'''
 
# '''Backslash'''
 
# '''Backslash'''
 
 
  
 
|-
 
|-
Line 321: Line 384:
  
 
'''Double Quote'''
 
'''Double Quote'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| * '''Double quote '''substitutes the value of '''variables''' and '''commands'''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"|  
 +
* '''Double quote '''substitutes the value of '''variables''' and '''commands'''
 
* Example: '''echo “Username is $USER”'''
 
* Example: '''echo “Username is $USER”'''
* It displays your '''username '''on the system.
+
* It displays the '''username '''on the system.
  
 +
|-
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Switch to '''terminal>>'''Type clear>> press Enter'''
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Switch to '''Terminal.'''
  
 +
Let me clear the prompt.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Switch to '''terminal>>'''Type '''echo “Username is $USER”>> press Enter'''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|'''Type '''echo “Username is $USER”>> press Enter'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let's switch to '''Terminal.'''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"|Now type
 +
'''echo space within double quotes Username space is dollar''USER'' ''' in capitals.
  
Type '''echo space opening double quote Username space is space dollar sign ''in capitals'' ''USER'' closing double quote '''
 
  
 
+
Press '''Enter'''
and Press '''Enter'''
+
  
 
|-
 
|-
Line 342: Line 409:
  
 
'''“Username is lavitha” '''
 
'''“Username is lavitha” '''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This will display your '''username '''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The '''username '''of the system is printed.
 +
 
 +
The output will vary according to your system.
  
 
'''<nowiki><<PAUSE>></nowiki>'''
 
'''<nowiki><<PAUSE>></nowiki>'''
  
'''Let us switch to the slides.'''
+
Now move back to slides.
  
 
|-
 
|-
Line 352: Line 421:
  
 
'''Single Quote'''
 
'''Single Quote'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| * '''Single quotes''' preserves the literal meaning of each character of the given string.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"|  
 +
* '''Single quotes''' preserves the literal meaning of each character of the given string.
 
* It is used to turn off special meaning of all characters.
 
* It is used to turn off special meaning of all characters.
 
 
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''Switch to terminal>>'''Type '''echo 'Username is $USER'>> press Enter'''
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''Switch to terminal>>'''Type '''echo 'Username is $USER'>> press Enter'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Switch to '''Terminal.'''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Switch to the '''Terminal.'''
  
'''T'''ype '''echo space open single quote Username space is space dollar sign ''in capitals USER'' close single quote'''
+
Type '''echo space within single quote Username is dollar USER''' in capitals
  
and Press '''Enter'''
+
Press '''Enter'''
  
 
|-
 
|-
Line 384: Line 452:
 
'''<nowiki><<PAUSE>></nowiki>'''
 
'''<nowiki><<PAUSE>></nowiki>'''
  
'''Switch back to the slides.'''
+
Switch back to our slides.
  
 
|-
 
|-
Line 391: Line 459:
  
 
'''Backslash'''
 
'''Backslash'''
 
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"|  
 
+
* '''Backslash''' removes the special meaning from a single character
 
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| * '''Backslash''' removes the special meaning from a single character
+
 
* It is used as an '''escape character''' in '''BASH'''
 
* It is used as an '''escape character''' in '''BASH'''
 
 
  
 
|-
 
|-
Line 403: Line 467:
  
  
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Switch to the '''Terminal. '''
  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Switch to '''Terminal. '''
+
Type '''echo space within double quote Username is backslash dollar USER ''' in capitals
  
Type '''echo space open double quote Username space is space backslash dollar sign ''in capitals USER'' close double quote '''
 
  
 
+
Since we have given double quotes, we expect the '''echo''' command to display the '''username'''.
Since we have given double quotes, we expect the '''echo''' command to display the username.
+
  
  
Line 433: Line 496:
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This brings us to the end of this tutorial.
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This brings us to the end of this tutorial.
  
Let us switch back to slides and summarize.
+
Let us switch back to our slides.
 +
 
 +
Let us summarize.
  
 
|-
 
|-
Line 443: Line 508:
 
* '''Command line arguments'''
 
* '''Command line arguments'''
 
* Functionality of '''Double quote''', '''Single quote '''and '''Backslash'''
 
* Functionality of '''Double quote''', '''Single quote '''and '''Backslash'''
 
 
  
 
|-
 
|-
Line 482: Line 545:
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Keep the same narration as Bash-1 and Bash-2 scripts for consistency.''Reply to nancy (26/06/2013, 11:35): "..."''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
 
+
Resolved
+
 
+
 
+
 
+
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The script has been contributed by FOSSEE and spoken-tutorial Team.
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The script has been contributed by FOSSEE and spoken-tutorial Team.
  

Latest revision as of 10:24, 16 July 2015

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


Visual Cue
Narration
Display Slide Welcome to the spoken tutorial on Command line arguments and Quoting in BASH
Display Slide In this tutorial, we will learn about
  • Command line Arguments and
  • Quoting
Display Slide

Prerequisites


spoken-tutorial.org

To follow this tutorial, you should be familiar with the 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 for practise.

Display slide :

Command Line arguments

  • Shell script can accept arguments from the command line.
  • An argument is passed to the program being called.
  • Any number of arguments can be passed to a program.
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


On the terminal, let me open this file by typing,

gedit space arg.sh space ampersand sign


We use the ampersand to free up the prompt.

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.

Switch to the terminal. Let us execute the program and see.

Switch to the terminal.

Type chmod plus x arg.sh First make it executable by typing,

chmod space plus x space arg.sh

and press Enter

Type

./arg.sh

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"

$2 (Dollar two) represents the second argument passed to the program.

[highlight] echo "third arg: $3" $3 (Dollar three) represents the third argument.
Click on Save. Now click on Save,
Press the uparrow key>> press Enter Let us execute the program and see.

Press the uparrow key and press Enter

We see that the zeroeth argument is printed.

But the first, second and third arguments are blank.

This is because the command line arguments are given during execution.

Press the uparrow key and type: sunday monday and tuesday. 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.


Else bash will only take the argument of the integer in the ten's place.


And you will not get expected output.

Click on Save. Now click on Save.
Switch to the terminal. Let us execute the program.

Switch to the terminal.

Type clear Let me clear the prompt.
Now we need to give 12 or 13 arguments to the program.
Type, dot slash arg.sh 1 to 13. Hence type, dot slash arg.sh 1 to 13.

Now press Enter

You can see that the 12th argument is 12.
Go back to the editor. 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 terminal. Switch to the terminal.
Let us execute.

Press the uparrow key and press Enter.

We can see that the total arguments are 13.
Switch to the editor. Now switch to the editor.

Press Enter and type the lines as shown here.

echo "Args(dollar *): $*"


for arg in “$*”

do

echo “$arg”

done

$* (Dollar asterix) will print all the arguments on a single line.


Highlight the for loop. We will test this with the help of a simple for loop.


We will analyse this for loop at the time of execution.

Now click on Save.

Switch to the terminal.

Type clear.

Let me clear the prompt.

Type, ./arg.sh sunday monday and tuesday 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 @): $@"


for arg in “$@”

do

echo “$arg”

done

$@ (Dollar at) will also print all the arguments.

However, this time each argument will be on a separate line.


Highlight the other for loop This is another for loop, which will print each argument in a separate line.
Let us see how. Click on Save

Switch to the terminal.

Press the uparrow key.

Press Enter

You can see the difference now.

These are the arguments printed by $@.

$@ prints each argument on separate 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
  1. Double quote
  2. Single quote
  3. Backslash
Display Slide

Double Quote

  • Double quote substitutes the value of variables and commands
  • Example: echo “Username is $USER”
  • It displays the username on the system.
Switch to terminal>>Type clear>> press Enter Switch to Terminal.

Let me clear the prompt.

Type echo “Username is $USER”>> press Enter Now type

echo space within double quotes Username space is dollarUSER in capitals.


Press Enter

Output:

Highlight

“Username is lavitha”

The username of the system is printed.

The output will 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.
  • It is used to turn off special meaning of all characters.
Switch to terminal>>Type echo 'Username is $USER'>> press Enter Switch to the Terminal.

Type echo space within single quote Username is dollar USER in capitals

Press Enter

Output


Highlight:

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


<<PAUSE>>

Switch back to our slides.

Display Slide


Backslash

  • Backslash removes the special meaning from a single character
  • It is used as an escape character in BASH
Switch to terminal>>Type echo Username is \$USER>> press Enter


Switch to the Terminal.

Type echo space within double quote Username is backslash dollar USER in capitals


Since we have given double quotes, we expect the echo command to display the username.


Let's try this command; so press Enter.

Output:

Highlight

Username is $USER

The output is Username is $USER

In this example the backslash removes the special meaning of (Dollar) $ symbol.


$USER is just treated as a string without any special functionality.

<<PAUSE>>

This brings us to the end of this tutorial.

Let us switch back to our slides.

Let us summarize.

Display slide

Summary

In this tutorial we learnt,
  • Command line arguments
  • Functionality of Double quote, Single quote and Backslash
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.


And this is Ashwini Patil from IIT Bombay signing off.

Thank you for joining.

Contributors and Content Editors

Ashwini, Gaurav, Nancyvarkey