0% found this document useful (0 votes)
8 views29 pages

08 Bash Basics

The document discusses various Bash basics including initialization files, aliases, variables, and command history. Initialization files read when starting login and non-login shells are described. Aliases allow creating shortcuts for commands and options. Variables influence shell behavior, and important internal Bash variables are listed.

Uploaded by

Meet Mahida
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views29 pages

08 Bash Basics

The document discusses various Bash basics including initialization files, aliases, variables, and command history. Initialization files read when starting login and non-login shells are described. Aliases allow creating shortcuts for commands and options. Variables influence shell behavior, and important internal Bash variables are listed.

Uploaded by

Meet Mahida
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Revision no.

: 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

– There are two different types of Shells :


• Login shells
• Started when a user logs in to system.
• The following files are read when starting a Login shell :
– /etc/profile : This files sets umask(determines file permissions defaults)and
number of different variables.
– ~/.bash_profile : Its stores user customizations, for ex: to change
command prompt. By default, this file is not present on the SUSE Linux
Enterprise Server.
– ~/.bash_login : The second user-specific file that Bash tries to find after
– ~/.bash_profile. By default, this file is also not created on SUSE Linux
Enterprise Server.
– ~/.profile : On the SUSE Linux Enterprise server, this file is created for each
new user by default.

© 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 (contd.)


4

• /etc/bash.bashrc : A system- wide configuration file of the SUSE Linux


Enterprise Server for Bash-specific settings, such as alias definitions
and special variables.

• Non-login shells
• Started from within a running Shell.
– ~/.bashrc : A configuration file read only by Bash in which users can store
their customizations.

– Linux distributions have a default setup that ensures users do


not see any difference between login shell and non-login shell.
– This achieved by reading ~/.bash.rc file when a login shell is
started.

© 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

– Aliases allows you to create shortcuts for command and their


options or to create commands with entirely different names.
– alias command can tell you about different aliases defined on
your system.
– For ex: alias | grep mkdir , command tells you that md is an
alias for mkdir .
– type command will show you that the given command is alias for
something else.
– type command will tell you whether it is in built-in shell
command, a regular command, or an alias.
• for ex : type –a ls
• ls is aliased to ‘ls $ LS_OPTIONS’
• ls is /bin/ls

© 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

– The example shows that ls, too is alias,


– The –a option was used for type to show both the contents of the
alias and the path to the original ls command.
– Aliases used on a system-wide basis are defined in the file
/etc/bash.bashrc.
– Aliases are defined by alias command and can be removed with
the unalias command.
– Entering unalias ls would remove the alias for ls , causing ls to
stop coloring its output.
– On the SuSE Linux ,the file ~/.alias is created for personal aliases
defined by each user.
– This file is read in by ~/.bashrc, where a command is included to
that effect.

© 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

– The behavior of the shell is influenced by its internal variables.


– Shell variables control the behavior of the shell itself and are only
relevant locally(for currently active shell).
– Environment variables have larger scope and also influence any
other programs started from within the current shell.
– They are inherited by any subshells or child processes.

• Working With Variables


– The syntax to set a shell variable is VARIABLE=value, by
which the contents of value are directly assigned to new variable.
– For ex:
• cms@localhost:-> VARIABLE1=“Good Morning”
• cms@localhost:-> echo $VARIABLE1
Good Morning

© 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

– With the command export, a shell variable can be turned into an


environment variable.
– you can use these commands ,to see which variable is set for
your shell;
• export Lists all environment variables.
• set Lists all variables as well as functions that been
declared.

• env Displays a list of all currently defined variables


and their contents.

– The command unset variable can be used to delete a variable

© 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

• Important Internal Bash Variables


• Variables Description
• HOME The user’s home directory.
• PATH The search path of commands.
• PWD Current working Directory
• IFS The internal field seprator
• PS1 The primary shell prompt
• PS2 The secondary shell prompt
• PS3 The tertiary shell prompt
• ? The exit status or of the most recent child process
• $ The process ID of the current shell itself.
• # The number of arguments passed to the shell
• 0-9 argument 0, argument 1 so on as passed to shell
• * all arguments (except argument 0) as a single word
• @ all arguments (except argument 0) as separate words

© 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.

– The variable HISTSIZE determines how many commands are kept


in the history and the variable HISTFILE determines to which file it
is saved.

– The default history file is ~/.bash_history and default size is set to


500.

– The command history prints a numbered list of all the command


stored in 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

Input and Output Channels


11

– Each programs relies on two standard data channels to receive


and another channel to output error messages.
– There are three Standard data channels
– Standard input(stdin) 0
– Standard output(stdout) 1
– Standard error(stderr) 2
– These numbers can be referenced to redirect the data channel.
– Each of the three I/O channels can be redirected independently
from the others.
– Redirection also allows to link up two processes by feeding the
output of the first process through a pipe then second process
takes this as its 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

Input and Output Channels (contd.)


12

• 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

Input and Output Channels (contd.)


13

• The here Operator <<


– The here operator “<<“ makes it possible to feed more than one
line of input to commands that read their input from the command
line.
• For ex: cms@localhost:-> sort << EOF

– 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

Input and Output Channels (contd.)


14

• Feeding Output to Another Process


– When we want to link up two programs such that the output
produced by a first program is read as the second program’s
input.
– For ex: If you want to use less pager to directly process the
output of ls -l
• cms@localhost:-> ls –l >filelist
• cms@localhost:-> less filelist
– Other option is you can also pipe.
– A pipe, represented by the ‘|’ symbol, can read the output of
process as available on stdout and directly feed it to another
standard input.
• cms@localhost:-> ls –l | less

© 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

Input and Output Channels (contd.)


15

• Chaining Commands With xargs


– The Bash shell as limited input buffer,where there are too long
arguments to be passed to given command.
– The commands aborts with an error.
– The syntax of xargs assumes that it receives a list of arguments
from a first command through pipe.
– The xargs command also need to be told about the program to
which it should pass the arguments of the first.
• command1 | xargs command2
– The buffer of xargs is larger than the shell’s buffer.
– So xargs first collects all arguments passed until its buffer is full,
then it calls the second command to supply these arguments to it.

© 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

Input and Output Channels (contd.)


16

• Duplicating the output with tee


– Program which writes to standard output and to a file at the same
time,it is done by tee command.

– A file name is specified as an argument for tee to tell it to which


file the second output stream should write.

– For ex:

• cms@localhost:-> find / -type f –name “*.c” | tee cfiles-found

– In above example it is possible to view output of the find


command and saving same output to file cfiles-found

© 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

– type tells you whether it is an alias, a built-in, or a command


external to the shell.
– Enter type –a to see all available information.
• cms@localhost:~> type –a ls

© 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

– Every command line entered in a Bash shell will be interpreted by


the Bash program first.
– These special characters include i.e spaces, the internal field
separator –the character separating individual arguments and the
“$” sign, which is used for variable substitution.
– There are different forms of quotation
• \ quotes the following Character
• “….” quotes any special characters enclosed with
exception of “$”.
• ‘ …’ quotes any special characters enclosed.

© 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

Substitution and Expansion


19

– When executing a command in the shell the entire command line


is evaluated by the shell while also interpreting any special
characters.
– The latter includes various forms of substitution and expansion-
among other things the shell replaces variables with their values
and expands metacharacters to file names.(called as file globbing)
• Variable Substitution
– Whenever the shell encounters a “$”it assumes a variable will
follow then replaces the variable with its value.
– Example: for variable substitution:
• cms@localhost:-> echo $USER
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

Substitution and Expansion (contd.)

20

• Filename Expansion (File Globbing)


– When specifying a file name, it is possible to use wild cards or
metacharacters representing characters to match.
– These are the metacharacters used by the shell for filename
expansion:
• Metacharacter Expands to /Matches
• ? any single Character(except ‘.’ & ‘/’).
• * any string(including null characters).
• […] any of the characters enclosed.

• [!..] none of the characters enclosed.

© 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

Substitution and Expansion (contd.)

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

Substitution and Expansion (contd.)

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

Substitution and Expansion (contd.)

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

Substitution and Expansion (contd.)

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

Substitution and Expansion (contd.)

25

– When declaring variable, they can give certain attribute. Variables


are declared using declare command.
• declare –i integer1

• 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

– When processing a command line, the shell interprets in following


order ;
• The command line is parsed up to the first command separator.
• The shell splits the command line into individual words.
• Variables are expanded to their values.
• The shell executes commands marked for substitution with
‘..’or$(..)replacing them with their output.
• Input and output redirection is performed.
• Any new variables are assigned.
• Special characters are interpreted and matched file name are
substituted.
• The command is executed.

© 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 (contd.)


27

– 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

Command-Line Interpretation (contd.)


28

• Command1 && command2

– The second command is only started when first command is successful.

• Command1 || command2

– The second commands is only started if first command fails.

– for ex:

• cms@localhost:~> w ; date

– Second command is executed after the first.

© 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

Design & Published by:


CMS Institute, Design & Development Centre, CMS House, Plot No. 91, Street No.7,
MIDC, Marol, Andheri (E), Mumbai –400093, Tel: 91-22-28216511, 28329198
Email: [email protected]
www.cmsinstitute.co.in
© 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

You might also like