0% found this document useful (0 votes)
48 views

BC Unix

The document discusses the bc command in Unix/Linux which is used for arithmetic calculations. It provides examples of various arithmetic, assignment, comparison and logical operators as well as functions like if/else, for loops, while loops and user-defined functions supported by the bc command.

Uploaded by

prateek
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)
48 views

BC Unix

The document discusses the bc command in Unix/Linux which is used for arithmetic calculations. It provides examples of various arithmetic, assignment, comparison and logical operators as well as functions like if/else, for loops, while loops and user-defined functions supported by the bc command.

Uploaded by

prateek
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/ 11

3/4/2016

bcCommandExamplesinUnix/Linuxtutorials

Home

DataWarehouse

Informatica

InformaticaScenarios

Oracle

Unix

Hadoop

bcCommandExamplesinUnix/Linuxtutorials
Arithmeticoperationsarethemostcommoninanykindofprogramminglanguage.Unixor
linuxoperatingsystemprovidesthebccommandandexprcommandfordoingarithmetic

Search...

Search

PopularPosts
SedCommandinUnixandLinuxExamples

calculations.Youcanusethesecommandsinbashorshellscriptalsoforevaluating
arithmeticexpressions.
Herewewillseeonlyaboutthebccommand.Thebccommandevaluatesexpressionssimilar
tothecprogramminglanguage.Thebccommandsupportsthefollowingfeatures.
Arithmeticoperators
Incrementanddecrementoperators
Assignmentoperators
ComparisionorRelationalOperators
LogicalorBooleanoperators
MathFunctions
Conditionalstatements
Iterativestatements
Functions

CutCommandinUnix(Linux)Examples
TopExamplesofAwkCommandinUnix
FindCommandinUnixandLinuxExamples
InformaticaScenarioBasedInterviewQuestionswith
AnswersPart1
DateFunctionsinHive
GrepCommandinUnixandLinuxExamples
StringFunctionsinHive
SQLQueriesInterviewQuestionsOraclePart1
TypesofDimensionsindatawarehouse

ArithmeticoperatorExamples:
Thefollowingexampleshowshowtousevariousarithmeticoperators.Theexamplesare

HaveQuestions?FollowMe

prettystraightforward.So,Iwillprovideexplanationonlywhenrequired.Inmostofthe
examplestheechostatmentisusedtoprovidetheexpressionstothebccommand.
http://www.folkstalk.com/2012/09/bccommandexamplesinunixlinux.html

1/11

3/4/2016

bcCommandExamplesinUnix/Linuxtutorials

vijaybhaskar
1.FindingSumofTwoexpressions

Addtocircles

>echo"2+5"|bc
7

2.DifferenceofTwonumbers

>echo"104"|bc
6
604havemeincircles

Viewall

3.Multiplyingtwonumbers

>echo"3*8"|bc
24

4.Dividingtwonumbers
Whenyoudividetwonumbers,thebccommandIgnoresthedecimalpartandreturnsonly
theintegralpartastheoutput.Seethebelowexamples

>echo"2/3"|bc
0
http://www.folkstalk.com/2012/09/bccommandexamplesinunixlinux.html

2/11

3/4/2016

bcCommandExamplesinUnix/Linuxtutorials

>echo"5/4"|bc
1

Usethescalefunctiontospecifythenumberofdecimaldigitsthatthebccommandshould
return.

>echo"scale=2;2/3"|bc
.66

5.Findingtheremainderusingmodulusoperator

>echo"6%4"|bc
2

6.Usingexponentoperator

>echo"10^2"|bc
100

Heretheexpressionisevaluatedas10tothepowerof2.
AssignmentOperatorExamples:
http://www.folkstalk.com/2012/09/bccommandexamplesinunixlinux.html

3/11

3/4/2016

bcCommandExamplesinUnix/Linuxtutorials

Assignmentoperatorsareusedtoassignavaluetothevariable.Thefollowingexample
showshowtousetheassignmentoperators:

Assigns10tothevariableandprintsthevalueontheterminal.
>echo"var=10;var"|bc
Incrementthevalueofthevariableby5
>echo"var=10;var+=5;var|bc
15

Thelistsofassignmentoperatorssupportedare:
var=value:Assignthevaluetothevariable
var+=value:similartovar=var+value
var=value:similartovar=varvalue
var*=value:similartovar=var*value
var/=value:similartovar=var/value
var^=value:similartovar=var^value
var%=value:similartovar=var%value
IncrementOperatorExamples:
Therearetwokindsofincrementoperators.Theyarepreincrementandpostincrement
operators.
++var:Preincrementoperator.Thevariableisincrementedfirstandthenthe
resultofthevariableisused.
var++:Postincrementoperator.Theresultofthevariableisusedfirstandthen
thevariableisincremented.
http://www.folkstalk.com/2012/09/bccommandexamplesinunixlinux.html

4/11

3/4/2016

bcCommandExamplesinUnix/Linuxtutorials

>echo"var=5;++var"|bc
6
>echo"var=5;var++"|bc
5

Here,inthesecondexamplethevalueofvarisprintedfirstandthenitisincremented.See
thebelowexample,toseethecompleteincrementaleffect.

>echo"var=5;var++;var"|bc
5
6

DecrementOperatorExamples:
Similartotheincrementoperators,therearetwotypesofdecrementoperators.
var:Predecrementoperator.Thevariableisdecrementedfirstandthenthe
resultofthevariableisused.
var:Postdecrementoperator.Theresultofthevariableisusedfirstandthen
thevariableisdecremented.
>echo"var=5;var"|bc
4
>echo"var=5;var"|bc
5

RelationalOperatorsExamples:
http://www.folkstalk.com/2012/09/bccommandexamplesinunixlinux.html

5/11

3/4/2016

bcCommandExamplesinUnix/Linuxtutorials

Relationaloperatorsareusedtocomparetwonumbers.Ifthecomparisonistrue,thenit
returns1.Otherwise(false),itreturns0.Therelationaloperatorsaremostlyusedin
conditionalstatementslikeif.Thelistofrelationaloperatorssupportedinbccommandare
shownbelow:
expr1<expr2:Resultis1ifexpr1isstrictlylessthanexpr2.
expr1<=expr2:Resultis1ifexpr1islessthanorequaltoexpr2.
expr1>expr2:Resultis1ifexpr1isstrictlygreaterthanexpr2.
expr1>=expr2:Resultis1ifexpr1isgreaterthanorequaltoexpr2.
expr1==expr2:Resultis1ifexpr1isequaltoexpr2.
expr1!=expr2:Resultis1ifexpr1isnotequaltoexpr2.

>echo"10>5"|bc
1
>echo"1==2"|bc
0

LogicalOperatorExamples:
Logicaloperatorsarealsomostlyusedinconditionalstatements.Theresultofthelogical
operatorsiseither1(True)or0(false)!expr:Resultis1ifexpris0.
expr&&expr:Resultis1ifbothexpressionsarenonzero.
expr||expr:Resultis1ifeitherexpressionisnonzero.

http://www.folkstalk.com/2012/09/bccommandexamplesinunixlinux.html

6/11

3/4/2016

bcCommandExamplesinUnix/Linuxtutorials

>echo"4&&10"|bc
1
>echo"0||0"|bc
0

MathFunctions:
Thebuiltinmathfunctionssupportedare:
s(x):Thesineofx,xisinradians.
c(x):Thecosineofx,xisinradians.
a(x):Thearctangentofx,arctangentreturnsradians.
l(x):Thenaturallogarithmofx.
e(x):Theexponentialfunctionofraisingetothevaluex.
j(n,x):Thebesselfunctionofintegerordernofx.
sqrt(x):Squarerootofthenumberx.
Inadditiontothemathfunctions,thefollowingfunctionsarealsosupported.
length(x):returnsthenumberofdigitsinx
read():Readsthenumberfromthestandardinput.
ConditionalStatementExamples:
Conditionalstatementsareusedtotakedecisionsandexecutestatementsbasedonthese
decisions.Bccommandsupportstheifcondition.Thesyntaxofifstatementis

if(condition){statements}else{statements}
http://www.folkstalk.com/2012/09/bccommandexamplesinunixlinux.html

7/11

3/4/2016

bcCommandExamplesinUnix/Linuxtutorials

Thefollowingexampleshowsshowtousetheifcondition

>echo'if(1==2)print"true"elseprint"false"'|bc
false

IterativeStatements:
Bccommandsupportstheforandwhileloopfordoingiterations.Thesyntaxofforandwhile
loopareshownbelow:

for(assignment;condition;increment){
statements
}
while(condition){
statements
}

Thefollowingexamplesprintsnumbersfrom1to10usingtheforandwhileloops

>echo"for(i=1;i<=10;i++){i;}"|bc
>echo"i=1;while(i<=10){i;i+=1}"|bc

Functions:
http://www.folkstalk.com/2012/09/bccommandexamplesinunixlinux.html

8/11

3/4/2016

bcCommandExamplesinUnix/Linuxtutorials

Afunctionisacodeblockwhichexecuteslogicallyrelatedfunctionalityandreturnsavalue.
Thesyntaxofcreatingafunctionis

definefunctionname(commaseparatedparameterslist){
statements
returnstatement
}

Sofarwehaveprovidedthearithmeticexpressionstothebccommandbyusingtheecho
statement.Wecanwritethesearithmeticexpressionsinafileandthenexecutethose
statementsbyprovidingthefilenametothebccommand.Thisisshownbelow:

>catarth_expr.dat
2+5;
var=10*3
var
printvar
definesum(a,b){
returna+b
}
sum(3,5)
quit

Nowseehowtoexecutethesestatements:

http://www.folkstalk.com/2012/09/bccommandexamplesinunixlinux.html

9/11

3/4/2016

bcCommandExamplesinUnix/Linuxtutorials

>bcarth_expr.dat
bc1.06
Copyright19911994,1997,1998,2000FreeSoftwareFoundation,Inc.
ThisisfreesoftwarewithABSOLUTELYNOWARRANTY.
Fordetailstype`warranty'.
7
30
30
8

Bedefaultthebccommandprintsthewelcomemessage(version,copyrightmessage.You
cansuppressthiswelcomemessagebyusingtheqoptionwithbccommand

>bcqarth_expr.dat

ImportantPoints:
Bccommandtreatsthesemicolon()ornewlineasthestatementseparator.
Togroupstatementsusethecurlybraces.Usewithfunctions,ifstatement,forand
whileloops.
Ifonlyanexpressionisspecifiedasastatement,thenbccommandevaluatesthe
expressionandprintstheresultonthestandardoutput.
Ifanassignmentoperatorisfound.Bccommandassignsthevaluetothevariable
anddonotprintthevalueontheterminal.
Afunctionshouldbedefinedbeforecallingit.Alwaysthefunctiondefinitionshould
appearfirstbeforethecallingstatements.
Ifastandalonevariableisfoundasastatement,bccommandprintsthevalueof
thevariable.YoucanalsoUsetheprintstatementfordisplayingthelistofvalues
http://www.folkstalk.com/2012/09/bccommandexamplesinunixlinux.html

10/11

3/4/2016

bcCommandExamplesinUnix/Linuxtutorials

ontheterminal.

NewerPost

Home

pnrstatus

http://www.folkstalk.com/2012/09/bccommandexamplesinunixlinux.html

OlderPost

privacypolicy

11/11

You might also like