SlideShare a Scribd company logo
1 of 9
Download to read offline
Confidentiality Statement
This document contains confidential information of Tata Consultancy Services
Limited, which is provided for the sole purpose of permitting the recipient to
evaluate the proposal submitted herewith. In consideration of receipt of this
document, the recipient agrees to maintain such information in confidence and to
not reproduce or otherwise disclose this information to any person outside the
group directly responsible for evaluation of its contents, except that there is no
obligation to maintain the confidentiality of any information which was known to
the recipient prior to receipt of such information from Tata Consultancy Services
Limited, or becomes publicly known through no fault of recipient, or is received
without obligation of confidentiality from a third party owing no obligation of
confidentiality to Tata Consultancy Services Limited.
Tata Code of Conduct
We, in our dealings, are self-regulated by a Code of Conduct as enshrined in the
Tata Code of Conduct. We request your support in helping us adhere to the Code
in letter and spirit. We request that any violation or potential violation of the Code
by any person be promptly brought to the notice of the Local Ethics Counselor or
the Principal Ethics Counselor or the CEO of TCS. All communication received
in this regard will be treated and kept as confidential.
TATA CONSULTANCY SERVICES
Virtual ILP – Introduction To Shell
Content Manual Version 1.1
December 2014
(ILP Guwahati)
Table of Content
1.Wild Card Characters................................................................................................................................................5
2.Shell Quoting Mechanism.........................................................................................................................................6
2.1.The Single Quotes..................................................................................................................................................7
2.2.The Double Quotes................................................................................................................................................8
2.3.Back Quotes: Command Substitution....................................................................................................................9
1. Wild Card Characters
Symbol used to replace or represent one or more characters. Wildcards or wild characters are either asterisk
(*), which represent one or more characters or question mark (?), which represent a single character.
Wild card
/Shorthand
Meaning Examples
*
Matches any string or group
of characters.
$ ls * will show all files
$ ls a*
will show all files whose first name is starting
with letter 'a'
$ ls *.c will show all files having extension .c
$ ls ut*.c
Will show all files having extension .c but file
name must begin with 'ut'.
? Matches any single character.
$ ls ?
will show all files whose names are 1 character
long
$ ls fo?
will show all files whose names are 3 character
long and file name begin with fo
[...]
Matches any one of the
enclosed characters
$ ls [abc]* Will show all files beginning with letters a,b,c
Note: [..-..] A pair of characters separated by a minus sign denotes a range.
Example:
$ ls /bin/[a-c]*
Will show all File name beginning with letter a,b or c.
TCS Internal
Output:
/bin/arch /bin/awk /bin/bsh /bin/chmod /bin/cp /bin/as /bin/basename /bin/cat
/bin/chown /bin/ash.static /bin/bash /bin/chgrp /bin/consolechars /bin/csh
2. Shell Quoting Mechanism
The Metacharacters
Unix Shell provides various metacharacters which have special meaning while using them in any Shell
Script and causes termination of a word unless quoted.
Example:
? Matches with a single character while listing files in a directory and an * would match more
than one characters.
Here is a list of most of the shell special characters (also called metacharacters):
* ? [ ] ' "  $ ; & ( ) | ^ < > new-line space tab
A character may be quoted (i.e., made to stand for itself) by preceding it with a .
Example:
#!/bin/sh
echo Hello; Word
This would produce following result.
Hello ./test.sh: line 2: Word: command not found
shell returned 127
Now let us try using a quoted character:
TCS Internal
#!/bin/sh
echo Hello; Word
This would produce following result:
Hello; Word
The $ sign is one of the metacharacters, so it must be quoted to avoid special handling by the
shell:
#!/bin/sh
echo "I have $1200"
This would produce following result:
I have $1200
Quoting Description
Single quote All special characters between these quotes lose their special meaning.
Double quote Most special characters between these quotes lose their special meaning with these ex-
ceptions:
$
`
$
'
"

Backslash Any character immediately following the backslash loses its special meaning.
Back Quote Anything in between back quotes would be treated as a command and would be ex-
ecuted.
2.1. The Single Quotes
Consider an echo command that contains many special shell characters:
echo <-$1500.**>; (update?) [y|n]
TCS Internal
Putting a backslash in front of each special character is tedious and makes the line difficult to read:
echo <-$1500.**>; (update?) [y|n]
There is an easy way to quote a large group of characters. Put a single quote ( ') at the beginning and
at the end of the string:
echo '<-$1500.**>; (update?) [y|n]'
Any characters within single quotes are quoted just as if a backslash is in front of each character. So
now this echo command displays properly.
If a single quote appears within a string to be output, you should not put the whole string within
single quotes instead you would precede that using a backslash () as follows:
echo 'It's Shell Programming'
2.2. The Double Quotes
Try to execute the following shell script. This shell script makes use of single quote:
VAR=ZARA
echo '$VAR owes <-$1500.**>; [ as of (`date +%m/%d`) ]'
This would produce following result:
$VAR owes <-$1500.**>; [ as of (`date +%m/%d`) ]
So this is not what you wanted to display. It is obvious that single quotes prevent variable substitution. If
you want to substitute variable values and to make invert commas work as expected then you would need to
put your commands in double quotes as follows:
VAR=ZARA
echo "$VAR owes <-$1500.**>; [ as of (`date +%m/%d`) ]"
Now this would produce following result:
ZARA owes <-$1500.**>; [ as of (07/02) ]
TCS Internal
Double quotes take away the special meaning of all characters except the following:
 $ for parameter substitution.
 Backquotes for command substitution.
 $ to enable literal dollar signs.
 ` to enable literal backquotes.
 " to enable embedded double quotes.
  to enable embedded backslashes.
 All other  characters are literal (not special).
Any characters within single quotes are quoted just as if a backslash is in front of each character. So now
this echo command displays properly.
2.3. Back Quotes: Command Substitution
Putting any Shell command in between back quotes would execute the command
Syntax:
var=`command`
Example:
Following would execute date command and produced result would be stored in DATA variable.
DATE=`date`
echo "Current Date: $DATE"
This would produce following result:
Current Date: Thu Jul 2 05:28:45 MST 2009
TCS Internal

More Related Content

What's hot

Array,lists and hashes in perl
Array,lists and hashes in perlArray,lists and hashes in perl
Array,lists and hashes in perlsana mateen
 
PowerShell 101
PowerShell 101PowerShell 101
PowerShell 101Thomas Lee
 
Think sharp, write swift
Think sharp, write swiftThink sharp, write swift
Think sharp, write swiftPascal Batty
 
Php basics
Php basicsPhp basics
Php basicshamfu
 
Ceylon idioms by Gavin King
Ceylon idioms by Gavin KingCeylon idioms by Gavin King
Ceylon idioms by Gavin KingUnFroMage
 
Practical approach to perl day2
Practical approach to perl day2Practical approach to perl day2
Practical approach to perl day2Rakesh Mukundan
 
PHP MATERIAL
PHP MATERIALPHP MATERIAL
PHP MATERIALzatax
 
perl usage at database applications
perl usage at database applicationsperl usage at database applications
perl usage at database applicationsJoe Jiang
 
Data structure in perl
Data structure in perlData structure in perl
Data structure in perlsana mateen
 
Best practices naming conventions
Best practices naming conventionsBest practices naming conventions
Best practices naming conventionsbluescreen10
 

What's hot (15)

Array,lists and hashes in perl
Array,lists and hashes in perlArray,lists and hashes in perl
Array,lists and hashes in perl
 
PHP Web Programming
PHP Web ProgrammingPHP Web Programming
PHP Web Programming
 
PowerShell 101
PowerShell 101PowerShell 101
PowerShell 101
 
Think sharp, write swift
Think sharp, write swiftThink sharp, write swift
Think sharp, write swift
 
Lecture 7
Lecture 7Lecture 7
Lecture 7
 
Php basics
Php basicsPhp basics
Php basics
 
Ceylon idioms by Gavin King
Ceylon idioms by Gavin KingCeylon idioms by Gavin King
Ceylon idioms by Gavin King
 
Practical approach to perl day2
Practical approach to perl day2Practical approach to perl day2
Practical approach to perl day2
 
Introduction in php part 2
Introduction in php part 2Introduction in php part 2
Introduction in php part 2
 
PHP MATERIAL
PHP MATERIALPHP MATERIAL
PHP MATERIAL
 
perl usage at database applications
perl usage at database applicationsperl usage at database applications
perl usage at database applications
 
Sorting arrays in PHP
Sorting arrays in PHPSorting arrays in PHP
Sorting arrays in PHP
 
Diving deep into twig
Diving deep into twigDiving deep into twig
Diving deep into twig
 
Data structure in perl
Data structure in perlData structure in perl
Data structure in perl
 
Best practices naming conventions
Best practices naming conventionsBest practices naming conventions
Best practices naming conventions
 

Viewers also liked

2_Unix_Command_Usage_-_1
2_Unix_Command_Usage_-_12_Unix_Command_Usage_-_1
2_Unix_Command_Usage_-_1Gautam Raja
 
2_Connecting_to_Unix_Server
2_Connecting_to_Unix_Server2_Connecting_to_Unix_Server
2_Connecting_to_Unix_ServerGautam Raja
 
4_Users_and_File_Permission_and_Directory_Commands
4_Users_and_File_Permission_and_Directory_Commands4_Users_and_File_Permission_and_Directory_Commands
4_Users_and_File_Permission_and_Directory_CommandsGautam Raja
 
5_File_Handling_Commands__vi_editor_and_environment_variables
5_File_Handling_Commands__vi_editor_and_environment_variables5_File_Handling_Commands__vi_editor_and_environment_variables
5_File_Handling_Commands__vi_editor_and_environment_variablesGautam Raja
 
3_Unix_Command_Usage_-_2
3_Unix_Command_Usage_-_23_Unix_Command_Usage_-_2
3_Unix_Command_Usage_-_2Gautam Raja
 
4_Unix_Command_Usage_-_3
4_Unix_Command_Usage_-_34_Unix_Command_Usage_-_3
4_Unix_Command_Usage_-_3Gautam Raja
 
1_Introduction_To_Unix_and_Basic_Unix_Commands
1_Introduction_To_Unix_and_Basic_Unix_Commands1_Introduction_To_Unix_and_Basic_Unix_Commands
1_Introduction_To_Unix_and_Basic_Unix_CommandsGautam Raja
 
1_Editors_in_Unix
1_Editors_in_Unix1_Editors_in_Unix
1_Editors_in_UnixGautam Raja
 

Viewers also liked (11)

2_Unix_Command_Usage_-_1
2_Unix_Command_Usage_-_12_Unix_Command_Usage_-_1
2_Unix_Command_Usage_-_1
 
2_Connecting_to_Unix_Server
2_Connecting_to_Unix_Server2_Connecting_to_Unix_Server
2_Connecting_to_Unix_Server
 
infoTest
infoTestinfoTest
infoTest
 
4_Users_and_File_Permission_and_Directory_Commands
4_Users_and_File_Permission_and_Directory_Commands4_Users_and_File_Permission_and_Directory_Commands
4_Users_and_File_Permission_and_Directory_Commands
 
5_File_Handling_Commands__vi_editor_and_environment_variables
5_File_Handling_Commands__vi_editor_and_environment_variables5_File_Handling_Commands__vi_editor_and_environment_variables
5_File_Handling_Commands__vi_editor_and_environment_variables
 
3_Use_of_WinSCP
3_Use_of_WinSCP3_Use_of_WinSCP
3_Use_of_WinSCP
 
3_Unix_Command_Usage_-_2
3_Unix_Command_Usage_-_23_Unix_Command_Usage_-_2
3_Unix_Command_Usage_-_2
 
4_Unix_Command_Usage_-_3
4_Unix_Command_Usage_-_34_Unix_Command_Usage_-_3
4_Unix_Command_Usage_-_3
 
1_Introduction_To_Unix_and_Basic_Unix_Commands
1_Introduction_To_Unix_and_Basic_Unix_Commands1_Introduction_To_Unix_and_Basic_Unix_Commands
1_Introduction_To_Unix_and_Basic_Unix_Commands
 
1_Editors_in_Unix
1_Editors_in_Unix1_Editors_in_Unix
1_Editors_in_Unix
 
Rpp lingkaran
Rpp lingkaranRpp lingkaran
Rpp lingkaran
 

Similar to 1_Introduction_to_shell

What is the general format for a Try-Catch block Assume that amt l .docx
 What is the general format for a Try-Catch block  Assume that amt l .docx What is the general format for a Try-Catch block  Assume that amt l .docx
What is the general format for a Try-Catch block Assume that amt l .docxajoy21
 
Advanced perl finer points ,pack&amp;unpack,eval,files
Advanced perl   finer points ,pack&amp;unpack,eval,filesAdvanced perl   finer points ,pack&amp;unpack,eval,files
Advanced perl finer points ,pack&amp;unpack,eval,filesShankar D
 
Csharp4 strings and_regular_expressions
Csharp4 strings and_regular_expressionsCsharp4 strings and_regular_expressions
Csharp4 strings and_regular_expressionsAbed Bukhari
 
Javascript part1
Javascript part1Javascript part1
Javascript part1Raghu nath
 
Java Programming Introduction Lexer 1 In this project we.pdf
Java Programming  Introduction Lexer 1 In this project we.pdfJava Programming  Introduction Lexer 1 In this project we.pdf
Java Programming Introduction Lexer 1 In this project we.pdfadinathassociates
 
Case, Loop & Command line args un Unix
Case, Loop & Command line args un UnixCase, Loop & Command line args un Unix
Case, Loop & Command line args un UnixVpmv
 
Twig Brief, Tips&Tricks
Twig Brief, Tips&TricksTwig Brief, Tips&Tricks
Twig Brief, Tips&TricksAndrei Burian
 
Mysqlppt
MysqlpptMysqlppt
MysqlpptReka
 
CSE 220 Assignment 3 Hints and Tips Some hints for approa.docx
CSE 220 Assignment 3 Hints and Tips  Some hints for approa.docxCSE 220 Assignment 3 Hints and Tips  Some hints for approa.docx
CSE 220 Assignment 3 Hints and Tips Some hints for approa.docxfaithxdunce63732
 
1.getting started with c
1.getting started with c1.getting started with c
1.getting started with cHardik gupta
 
sbt-ethereum: a terminal for the world computer
sbt-ethereum: a terminal for the world computersbt-ethereum: a terminal for the world computer
sbt-ethereum: a terminal for the world computerSteve Waldman
 

Similar to 1_Introduction_to_shell (20)

What is the general format for a Try-Catch block Assume that amt l .docx
 What is the general format for a Try-Catch block  Assume that amt l .docx What is the general format for a Try-Catch block  Assume that amt l .docx
What is the general format for a Try-Catch block Assume that amt l .docx
 
Shell programming
Shell programmingShell programming
Shell programming
 
Shell scripting
Shell scriptingShell scripting
Shell scripting
 
SHELL PROGRAMMING
SHELL PROGRAMMINGSHELL PROGRAMMING
SHELL PROGRAMMING
 
First steps in C-Shell
First steps in C-ShellFirst steps in C-Shell
First steps in C-Shell
 
Advanced perl finer points ,pack&amp;unpack,eval,files
Advanced perl   finer points ,pack&amp;unpack,eval,filesAdvanced perl   finer points ,pack&amp;unpack,eval,files
Advanced perl finer points ,pack&amp;unpack,eval,files
 
Csharp4 strings and_regular_expressions
Csharp4 strings and_regular_expressionsCsharp4 strings and_regular_expressions
Csharp4 strings and_regular_expressions
 
Javascript part1
Javascript part1Javascript part1
Javascript part1
 
Java Programming Introduction Lexer 1 In this project we.pdf
Java Programming  Introduction Lexer 1 In this project we.pdfJava Programming  Introduction Lexer 1 In this project we.pdf
Java Programming Introduction Lexer 1 In this project we.pdf
 
Case, Loop & Command line args un Unix
Case, Loop & Command line args un UnixCase, Loop & Command line args un Unix
Case, Loop & Command line args un Unix
 
Twig Brief, Tips&Tricks
Twig Brief, Tips&TricksTwig Brief, Tips&Tricks
Twig Brief, Tips&Tricks
 
Unix shell scripting tutorial
Unix shell scripting tutorialUnix shell scripting tutorial
Unix shell scripting tutorial
 
part2.pdf
part2.pdfpart2.pdf
part2.pdf
 
Namespaces
NamespacesNamespaces
Namespaces
 
Mysqlppt
MysqlpptMysqlppt
Mysqlppt
 
Spsl by sasidhar 3 unit
Spsl by sasidhar  3 unitSpsl by sasidhar  3 unit
Spsl by sasidhar 3 unit
 
Shell Scripting
Shell ScriptingShell Scripting
Shell Scripting
 
CSE 220 Assignment 3 Hints and Tips Some hints for approa.docx
CSE 220 Assignment 3 Hints and Tips  Some hints for approa.docxCSE 220 Assignment 3 Hints and Tips  Some hints for approa.docx
CSE 220 Assignment 3 Hints and Tips Some hints for approa.docx
 
1.getting started with c
1.getting started with c1.getting started with c
1.getting started with c
 
sbt-ethereum: a terminal for the world computer
sbt-ethereum: a terminal for the world computersbt-ethereum: a terminal for the world computer
sbt-ethereum: a terminal for the world computer
 

1_Introduction_to_shell

  • 1. Confidentiality Statement This document contains confidential information of Tata Consultancy Services Limited, which is provided for the sole purpose of permitting the recipient to evaluate the proposal submitted herewith. In consideration of receipt of this document, the recipient agrees to maintain such information in confidence and to not reproduce or otherwise disclose this information to any person outside the group directly responsible for evaluation of its contents, except that there is no obligation to maintain the confidentiality of any information which was known to the recipient prior to receipt of such information from Tata Consultancy Services Limited, or becomes publicly known through no fault of recipient, or is received without obligation of confidentiality from a third party owing no obligation of confidentiality to Tata Consultancy Services Limited.
  • 2. Tata Code of Conduct We, in our dealings, are self-regulated by a Code of Conduct as enshrined in the Tata Code of Conduct. We request your support in helping us adhere to the Code in letter and spirit. We request that any violation or potential violation of the Code by any person be promptly brought to the notice of the Local Ethics Counselor or the Principal Ethics Counselor or the CEO of TCS. All communication received in this regard will be treated and kept as confidential.
  • 3. TATA CONSULTANCY SERVICES Virtual ILP – Introduction To Shell Content Manual Version 1.1 December 2014 (ILP Guwahati)
  • 4. Table of Content 1.Wild Card Characters................................................................................................................................................5 2.Shell Quoting Mechanism.........................................................................................................................................6 2.1.The Single Quotes..................................................................................................................................................7 2.2.The Double Quotes................................................................................................................................................8 2.3.Back Quotes: Command Substitution....................................................................................................................9
  • 5. 1. Wild Card Characters Symbol used to replace or represent one or more characters. Wildcards or wild characters are either asterisk (*), which represent one or more characters or question mark (?), which represent a single character. Wild card /Shorthand Meaning Examples * Matches any string or group of characters. $ ls * will show all files $ ls a* will show all files whose first name is starting with letter 'a' $ ls *.c will show all files having extension .c $ ls ut*.c Will show all files having extension .c but file name must begin with 'ut'. ? Matches any single character. $ ls ? will show all files whose names are 1 character long $ ls fo? will show all files whose names are 3 character long and file name begin with fo [...] Matches any one of the enclosed characters $ ls [abc]* Will show all files beginning with letters a,b,c Note: [..-..] A pair of characters separated by a minus sign denotes a range. Example: $ ls /bin/[a-c]* Will show all File name beginning with letter a,b or c. TCS Internal
  • 6. Output: /bin/arch /bin/awk /bin/bsh /bin/chmod /bin/cp /bin/as /bin/basename /bin/cat /bin/chown /bin/ash.static /bin/bash /bin/chgrp /bin/consolechars /bin/csh 2. Shell Quoting Mechanism The Metacharacters Unix Shell provides various metacharacters which have special meaning while using them in any Shell Script and causes termination of a word unless quoted. Example: ? Matches with a single character while listing files in a directory and an * would match more than one characters. Here is a list of most of the shell special characters (also called metacharacters): * ? [ ] ' " $ ; & ( ) | ^ < > new-line space tab A character may be quoted (i.e., made to stand for itself) by preceding it with a . Example: #!/bin/sh echo Hello; Word This would produce following result. Hello ./test.sh: line 2: Word: command not found shell returned 127 Now let us try using a quoted character: TCS Internal
  • 7. #!/bin/sh echo Hello; Word This would produce following result: Hello; Word The $ sign is one of the metacharacters, so it must be quoted to avoid special handling by the shell: #!/bin/sh echo "I have $1200" This would produce following result: I have $1200 Quoting Description Single quote All special characters between these quotes lose their special meaning. Double quote Most special characters between these quotes lose their special meaning with these ex- ceptions: $ ` $ ' " Backslash Any character immediately following the backslash loses its special meaning. Back Quote Anything in between back quotes would be treated as a command and would be ex- ecuted. 2.1. The Single Quotes Consider an echo command that contains many special shell characters: echo <-$1500.**>; (update?) [y|n] TCS Internal
  • 8. Putting a backslash in front of each special character is tedious and makes the line difficult to read: echo <-$1500.**>; (update?) [y|n] There is an easy way to quote a large group of characters. Put a single quote ( ') at the beginning and at the end of the string: echo '<-$1500.**>; (update?) [y|n]' Any characters within single quotes are quoted just as if a backslash is in front of each character. So now this echo command displays properly. If a single quote appears within a string to be output, you should not put the whole string within single quotes instead you would precede that using a backslash () as follows: echo 'It's Shell Programming' 2.2. The Double Quotes Try to execute the following shell script. This shell script makes use of single quote: VAR=ZARA echo '$VAR owes <-$1500.**>; [ as of (`date +%m/%d`) ]' This would produce following result: $VAR owes <-$1500.**>; [ as of (`date +%m/%d`) ] So this is not what you wanted to display. It is obvious that single quotes prevent variable substitution. If you want to substitute variable values and to make invert commas work as expected then you would need to put your commands in double quotes as follows: VAR=ZARA echo "$VAR owes <-$1500.**>; [ as of (`date +%m/%d`) ]" Now this would produce following result: ZARA owes <-$1500.**>; [ as of (07/02) ] TCS Internal
  • 9. Double quotes take away the special meaning of all characters except the following:  $ for parameter substitution.  Backquotes for command substitution.  $ to enable literal dollar signs.  ` to enable literal backquotes.  " to enable embedded double quotes.  to enable embedded backslashes.  All other characters are literal (not special). Any characters within single quotes are quoted just as if a backslash is in front of each character. So now this echo command displays properly. 2.3. Back Quotes: Command Substitution Putting any Shell command in between back quotes would execute the command Syntax: var=`command` Example: Following would execute date command and produced result would be stored in DATA variable. DATE=`date` echo "Current Date: $DATE" This would produce following result: Current Date: Thu Jul 2 05:28:45 MST 2009 TCS Internal