1 00:00:01 --> 00:00:07 Welcome to the spoken tutorial on Command line arguments and Quoting in BASH 2 00:00:08 --> 00:00:10 In this tutorial, we will learn about 3 00:00:11 --> 00:00:12 * Command line Arguments and 4 00:00:13 --> 00:00:14 * Quoting 5 00:00:15 --> 00:00:19 To follow this tutorial, you should be familiar with the Linux Operating System. 6 00:00:20 --> 00:00:25 If not, for relevant tutorials please visit our website which as shown 7 00:00:26 --> 00:00:28 For this tutorial I am using 8 00:00:29 --> 00:00:32 * Ubuntu Linux 12.04 OS 9 00:00:33 --> 00:00:36 * GNU Bash version 4.1.10 10 00:00:37 --> 00:00:42 GNU Bash version 4 or above is recommended for practice. 11 00:00:43 --> 00:00:45 * Shell script can accept arguments from the command line. 12 00:00:46 --> 00:00:51 * An argument is passed to a program being called. 13 00:00:52 --> 00:00:56 * Any number of arguments can be passed to a program. 14 00:00:57 --> 00:01:05 Let us open the terminal by pressing Ctrl Alt andT keys simultaneously on your keyboard. 15 00:01:06 --> 00:01:11 I have already written the code in the file named arg.sh 16 00:01:12 --> 00:01:15 On the terminal, let me open this file by typing, 17 00:01:16 --> 00:01:22 gedit space arg.sh space ampersand sign 18 00:01:23 --> 00:01:26 We use the ampersand to free up the prompt. 19 00:01:27 --> 00:01:29 Now, Press Enter. 20 00:01:30 --> 00:01:32 The text editor is opened. 21 00:01:33 --> 00:01:35 Let me explain the code now. 22 00:01:36 --> 00:01:38 This is the shenbang line. 23 00:01:39 --> 00:01:42 This line will print the Zeroth argument. 24 00:01:43 --> 00:01:47 Here, $0 (Dollar zero) will print the name of the shell script. 25 00:01:48 --> 00:01:54 This in turn means that, the zeroth argument is the name of the program itself. 26 00:01:55 --> 00:01:58 Let us execute the program and see. 27 00:01:59 --> 00:02:00 Switch to the terminal. 28 00:02:01 --> 00:02:04 First make the file executable by typing, 29 00:02:05 --> 00:02:11 chmod space plus x space arg.sh 30 00:02:12 --> 00:02:13 Press Enter 31 00:02:14 --> 00:02:17 Now type dot slash arg.sh 32 00:02:18 --> 00:02:18 Press Enter 33 00:02:19 --> 00:02:25 The output is displayed as: Zeroth argument is arg.sh 34 00:02:26 --> 00:02:32 Now come back to our editor and type the three lines as shown here. 35 00:02:33 --> 00:02:39 $1 (Dollar one) represents the first argument passed to the program from the command line. 36 00:02:40 --> 00:02:43 $2 (Dollar two) represents the second argument passed to the program. 37 00:02:44 --> 00:02:47 And $3 (Dollar three) represents the third argument 38 00:02:48 --> 00:02:48 Now click on Save 39 00:02:49 --> 00:02:51 Let us execute the program and see. 40 00:02:52 --> 00:02:56 Press the uparrow key press Enter 41 00:02:57 --> 00:02:59 We see that the zeroeth argument is printed. 42 00:03:00 --> 00:03:04 But the first, second and third arguments are blank. 43 00:03:05 --> 00:03:10 This is because the command line arguments are given during execution. 44 00:03:11 --> 00:03:17 Hence press the uparrow key and type: sunday monday and tuesday. 45 00:03:18 --> 00:03:20 Press Enter 46 00:03:21 --> 00:03:27 You can see that the first second and third arguments are Sunday Monday and Tuesday 47 00:03:28 --> 00:03:32 Now switch back to our editor. Press Enter 48 00:03:33 --> 00:03:36 Now type the line as shown here. 49 00:03:37 --> 00:03:40 $12 (Dollar twelve) represents the twelveth argument. 50 00:03:41 --> 00:03:45 To write an argument greater than 9, we need to use curly brackets. 51 00:03:46 --> 00:03:52 Else bash will only take the argument of the integer in the ten's place. 52 00:03:53 --> 00:03:56 And you will not get expected output. 53 00:03:57 --> 00:03:58 Now click on Save. 54 00:03:59 --> 00:04:00 Let us execute the program. 55 00:04:01 --> 00:04:03 Switch to the terminal. 56 00:04:04 --> 00:04:06 Let me clear the prompt. 57 00:04:07 --> 00:04:11 Now we need to give 12 or 13 arguments to the program. 58 00:04:12 --> 00:04:22 Hence Type dot slash arg.sh space 1 to 13 Now press enter 59 00:04:23 --> 00:04:26 You can see that the 12th argument is 12. 60 00:04:27 --> 00:04:29 Come back to our editor. 61 00:04:30 --> 00:04:33 And type the line as shown here. 62 00:04:34 --> 00:04:39 $# (Dollar hash) gives the total number of arguments that have been passed to a program. 63 00:04:40 --> 00:04:42 Now click on Save. 64 00:04:43 --> 00:04:43 Let us execute. 65 00:04:44 --> 00:04:45 Switch to the terminal. 66 00:04:46 --> 00:04:51 Let us execute. Press the uparrow key and press Enter. 67 00:04:52 --> 00:04:56 We can see that the total arguments are 13. 68 00:04:57 --> 00:04:59 Now switch to the editor. 69 00:05:00 --> 00:05:03 Press Enter and type the lines as shown here. 70 00:05:04 --> 00:05:09 $* (Dollar asterix) will print all the arguments on a single line. 71 00:05:10 --> 00:05:13 We will test this with the help of a simple for loop. 72 00:05:14 --> 00:05:17 We will analyse this for loop at the time of execution. 73 00:05:18 --> 00:05:21 Now click on Save.Switch to the terminal. 74 00:05:22 --> 00:05:25 Let me clear the prompt. 75 00:05:26 --> 00:05:34 Now let us type, dot slash arg.sh space sunday monday and tuesday 76 00:05:35 --> 00:05:37 Press Enter 77 00:05:38 --> 00:05:45 You can see that the total number of arguments are 3 as we have passed 3 arguments to our program. 78 00:05:46 --> 00:05:53 As already said $* will print all the arguments on a single line. 79 00:05:54 --> 00:05:56 And this is the output for the for loop. 80 00:05:57 --> 00:06:01 We see that all the arguments are printed on the single line. 81 00:06:02 --> 00:06:08 Now move back to our program and type the lines as shown here. 82 00:06:09 --> 00:06:12 $@ (Dollar at) will also print all the arguments. 83 00:06:13 --> 00:06:19 However, this time each argument will be printed on separate line. 84 00:06:20 --> 00:06:25 This is another for loop, which will print each argument in a separate line. 85 00:06:26 --> 00:06:28 Let us see how. Click on Save 86 00:06:29 --> 00:06:31 Switch to the terminal. 87 00:06:32 --> 00:06:33 Press the uparrow key. 88 00:06:34 --> 00:06:38 Press Enter You can see the difference now. 89 00:06:39 --> 00:06:42 These are the arguments printed by $@. 90 00:06:43 --> 00:06:46 $@ prints each argument on separate line. 91 00:06:47 --> 00:06:51 This is the output for the 2nd for loop. 92 00:06:52 --> 00:06:54 Now lets move on to quoting in BASH 93 00:06:55 --> 00:06:56 Switch to the slides. 94 00:06:57 --> 00:06:58 There are three types of quotes 95 00:06:59 --> 00:06:59 Double quote 96 00:07:00 --> 00:07:01 Single quote 97 00:07:02 --> 00:07:02 Backslash 98 00:07:03 --> 00:07:08 * Double quote substitutes the value of variables and commands 99 00:07:09 --> 00:07:12 * Example echo “Username is $USER” 100 00:07:13 --> 00:07:16 * It displays your username of the system. 101 00:07:17 --> 00:07:19 Switch to the Terminal. 102 00:07:20 --> 00:07:22 Let me clear the prompt. 103 00:07:23 --> 00:07:33 Now type echo space within double quotes Username space is dollar USER in capitals. 104 00:07:34 --> 00:07:34 Press Enter 105 00:07:35 --> 00:07:38 The username of the system is printed. 106 00:07:39 --> 00:07:41 The output will vary according to your system. 107 00:07:42 --> 00:07:45 Now move back to slides. 108 00:07:46 --> 00:07:52 * Single quotes preserves the literal meaning of each character of the given string. 109 00:07:53 --> 00:07:57 * It is used to turn off special meaning of all characters. 110 00:07:58 --> 00:08:00 Switch to the Terminal. 111 00:08:01 --> 00:08:09 Type echo space within single quote Username is dollar USER in capital 112 00:08:10 --> 00:08:11 Press Enter 113 00:08:12 --> 00:08:15 The output is Username is $USER 114 00:08:16 --> 00:08:22 In this example, it prints all the characters which appear within the single quotes. 115 00:08:23 --> 00:08:27 It does not substitute the value of variable $USER 116 00:08:28 --> 00:08:30 Switch back to our slides. 117 00:08:31 --> 00:08:36 * Backslash removes the special meaning from a single character 118 00:08:37 --> 00:08:41 * It is used as an escape character in BASH 119 00:08:42 --> 00:08:43 Switch to the Terminal. 120 00:08:44 --> 00:08:54 Now Type echo space within double quote Username is backslash dollar USER (in capital) 121 00:08:55 --> 00:09:01 Since we have given double quotes, we expect the echo command to display the username. 122 00:09:02 --> 00:09:05 Let's try this command so press Enter. 123 00:09:06 --> 00:09:09 The output is Username is $USER 124 00:09:10 --> 00:09:15 In this example the backslash removes the special meaning of (Dollar) $ symbol. 125 00:09:16 --> 00:09:21 $USER is just treated as a string without any special functionality. 126 00:09:22 --> 00:09:24 This brings us to the end of this tutorial. 127 00:09:25 --> 00:09:26 Switch back to our slides. 128 00:09:27 --> 00:09:27 Let us summarize. 129 00:09:28 --> 00:09:30 In this tutorial we learnt, 130 00:09:31 --> 00:09:32 * Command line arguments 131 00:09:33 --> 00:09:38 * Functionality of Double quote, Single quote and Backslash 132 00:09:39 --> 00:09:41 Watch the video available at the link shown below 133 00:09:42 --> 00:09:44 It summarises the Spoken Tutorial project 134 00:09:45 --> 00:09:50 If you do not have good bandwidth, you can download and watch it 135 00:09:51 --> 00:09:55 The Spoken Tutorial Project Team Conducts workshops using spoken tutorials 136 00:09:56 --> 00:09:59 Gives certificates to those who pass an online test 137 00:10:00 --> 00:10:06 For more details, please write to contact@spoken-tutorial.org 138 00:10:07 --> 00:10:09 Spoken Tutorial Project is a part of the Talk to a Teacher project 139 00:10:10 --> 00:10:23 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 140 00:10:24 --> 00:10:29 The script has been contributed by FOSSEE and spoken-tutorial Team. 141 00:10:30 --> 00:10:35 And this is Ashwini Patil from IIT Bombay signing off. Thank you for joining.