08 Bash Basics
08 Bash Basics
: PPT/2K403/02
Bash Basics
Revision no.: PPT/2K403/02
Bash Basics
2
• Initialization Files.
• Aliases.
• Variables.
• Command History.
• Input and Output Channels.
• Types of Commands.
• Quoting.
• Substitution and Expansion.
• Command –Line Interpretation.
© CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
Revision no.: PPT/2K403/02
Initialization Files
3
© CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
Revision no.: PPT/2K403/02
• Non-login shells
• Started from within a running Shell.
– ~/.bashrc : A configuration file read only by Bash in which users can store
their customizations.
© CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
Revision no.: PPT/2K403/02
Aliases
5
© CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
Revision no.: PPT/2K403/02
Aliases (contd.)
6
© CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
Revision no.: PPT/2K403/02
Variable
7
© CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
Revision no.: PPT/2K403/02
Variable (contd.)
8
© CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
Revision no.: PPT/2K403/02
Variable (contd.)
9
© CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
Revision no.: PPT/2K403/02
Command History
10
– Bash keeps a list of all the commands that have been entered in
the command history.
© CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
Revision no.: PPT/2K403/02
• Redirection to Files
– All three data channels can be redirected to files.
– A program could read the required parameters from a file instead
of the standard input.
– If you give command wc , it will print number of lines and
characters.
– The input can also be taken directly from a file.
– This requires the ‘<‘ operator to redirect the input.
• cms@localhost:-> wc < /etc/passwd
– This command will tell the program to read its input from
specified file, rather than from standard input.
© CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
Revision no.: PPT/2K403/02
– The sort command will take file name as one of its arguments then
arranges the input coming from the file in an alphabetical order
list.
– We use the here operator to feed the command with a list of
words directly from Keyboard.
© CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
Revision no.: PPT/2K403/02
© CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
Revision no.: PPT/2K403/02
© CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
Revision no.: PPT/2K403/02
– For ex:
© CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
Revision no.: PPT/2K403/02
Types of command
17
– The command exec and export are the types of commands called
shell built-ins.
– This commands are integral part of BASH itself and run as apart
of current shell, no child process is started by them.
– To check the type of command, use type command.
• cms@localhost:~> type cd
cd is a shell builtin
© CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
Revision no.: PPT/2K403/02
Quoting
18
© CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
Revision no.: PPT/2K403/02
© CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
Revision no.: PPT/2K403/02
20
© CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
Revision no.: PPT/2K403/02
21
• Brace Expansion
– Curly braces allow to specify a set of characters from which shell
will automatically form all possible combinations.
– Characters to be combined with given string must be specified as
comma-separated list with no spaces.
– For ex:
• cms@localhost:-> mkdir directory {1,2}{a,b}
• cms@localhost:-> ls
directory1a directory2a
directory1b directory2b
© CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
Revision no.: PPT/2K403/02
22
• Tilde Expansion
– With the help of tilde(“~”)the shell can refer to user’s home
directory
– The tilde can be used in two different ways :
• ~ Used as single character,the tilde expands to the home directory of
the user running shell.
• ~login-name If followed by a user(login) name,it expands to home
directory of that user without knowing actual path.
– Any user can refer to his home directory by specifying the path as
simple “~”
• cms@localhost:/tmp> cp somefile ~/somedirectory
• localhost:~ # cp /tmp/somefile ~cms
© CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
Revision no.: PPT/2K403/02
23
• Command Substitution
– To process the output of some command as an argument of
another command, is done by command substitution.
– Two possibilities to make sure that the shell processes the output
of another command in given command line.
• $(command) Bash-style command substitution
• ‘command’ command substitution.
– For ex:
• cms@localhost:~> echo “ It’s ‘date +%b-%d’ today.”
It’s May-24 today.
– cms@localhost:~> FELLAS=$(who)
– cms@localhost:~> echo $FELLAS
cms :0 May 24 04:50 (console) cms pts/0 Dec 20 04:50
© CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
Revision no.: PPT/2K403/02
24
• Arithmetic Substitution
– There are two different formats for arithmetic substitution.
• $[ INTEGER1 + INTEGER2 ] An arithmetic operation with one of the
operator “+”, “-”, “*”, or “/” or logical operation,such as AND, OR.
• $(( INTEGER1 – INTEGER2 )) An arithmetic operation with one of the
operators “+”, “-”, “*”, or “/” or logical AND,comparison.
– For ex:
• COUNTER=0
….
COUNTER=$(( COUNTER + 1)
© CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
Revision no.: PPT/2K403/02
25
• declare –i integer2
• declare –i sum
• ….
• SUM=integer1 + integer2
© CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
Revision no.: PPT/2K403/02
Command-Line Interpretation
26
© CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
Revision no.: PPT/2K403/02
– For ex:
– cms@localhost:~> echo files: ‘ls $HOME’ \;date: $(date) > outputfile
– The output of this command line is not printed on standard output,
but directed to the output file.
• Command Separators
– Commands can also be combined such that they are executed in
sequence, without their input and output connected to each other.
• Command1 ; command2
– The first command is executed, and second one is started as soon as first is
finished.
• Command1 & command2
– The first command is started in the background,the second one is started in
the foreground
© CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
Revision no.: PPT/2K403/02
• Command1 || command2
– for ex:
• cms@localhost:~> w ; date
© CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
Revision no.: PPT/2K403/02
29