# Linux Command Directory

This directory of Linux commands is from Linux in a Nutshell, 5th Edition.

Click on any of the 687 commands below to get a description and list of available options. All links in the command summaries point to the online version of the book on Safari Bookshelf.

Buy it now, or read it online on Safari Bookshelf.

## bc

bc [*options*] [*files*]

**bc** is a language (and compiler) whose syntax resembles that of C, but with unlimited-precision arithmetic. **bc** consists of identifiers

Interactively perform arbitrary-precision arithmetic or convert numbers from one base to another. Input can be taken from *files* or read from the standard input. To exit, type **quit** or EOF.

### Options

**-h**,

**--help**

Print help message and exit.

**-i**,

**--interactive**

Interactive mode.

**-l**,

**--mathlib**

Make functions from the math library available.

**-s**,

**--standard**

Ignore all extensions, and process exactly as in POSIX.

**-w**,

**--warn**

When extensions to POSIX

bcare used, print a warning.

**-q**,

**--quiet**

Do not display welcome message.

**-v**,

**--version**

Print version number.

### Identifiers

An identifier is a series of one or more characters. It must begin with a lowercase letter but may also contain digits and underscores. No uppercase letters are allowed. Identifiers are used as names for variables, arrays, and functions. Variables normally store arbitrary-precision numbers. Within the same program you may name a variable, an array, and a function using the same letter. The following identifiers would not conflict:

*x*

Variable

x.

*x*[

*i*]

Element

iof arrayx.ican range from 0 to 2047 and can also be an expression.

*x*(

*y*,

*z*)

Call function

xwith parametersyandz.

### Input-output keywords

**ibase**, **obase**, **scale**, and **last** store a value. Typing them on a line by themselves displays their current value. You can also change their values through assignment. The letters A-F are treated as digits whose values are 10-15.

**ibase =**

*n*

Numbers that are input (e.g., typed) are read as base

n(default is 10).

**obase =**

*n*

Numbers that are displayed are in base

n(default is 10). Note: onceibasehas been changed from 10, use A to restoreibaseorobaseto decimal.

**scale =**

*n*

Display computations using

ndecimal places (default is 0, meaning that results are truncated to integers).scaleis normally used only for base-10 computations.

**last**

Value of last printed number.

### Statement keywords

A semicolon or a newline separates one statement from another. Curly braces are needed when grouping multiple statements:

**if**

**(**rel-expr

**)**

**{**statements

**}**[

**else**

**{**

*statements*

**}**]

Do one or more

statementsif relational expressionrel-expris true. Otherwise, do nothing, or ifelse(an extension) is specified, do alternativestatements. For example:

if (x= =y) {i = i + 1} else {i = i - 1}

**while (**

*rel-expr*

**) {**

*statements*

**}**

Repeat one or more

statementswhilerel-expris true. For example:

while (i>0) {p = p*n; q = a/b; i = i-1}

**for (**expr1;

*rel-expr*;

*expr2*

**) {**statements

**}**

Similar to

while. For example, to print the first 10 multiples of 5, you could type:

for (i=1; i<=10; i++) i*5GNU

bcdoes not require three arguments tofor. A missing argument 1 or 3 means that those expressions will never be evaluated. A missing argument 2 evaluates to the value 1.

**break**

Terminate a

whileorforstatement.

*list*

GNU extension. It provides an alternate means of output.

listconsists of a series of comma-separated strings and expressions;last. Strings (which may contain special characters—i.e., characters beginning with \) are simply printed. Special characters can be:aAlert or bell

bBackspace

fForm feed

nNewline

rCarriage return

qDouble quote

tTab

\Backslash

**continue**

GNU extension. When within a

forstatement, jump to the next iteration.

**halt**

GNU extension. Cause the

bcprocessor to quit when executed.

**quit**

GNU extension. Cause the

bcprocessor to quit whether line is executed or not.

**limits**

GNU extension. Print the limits enforced by the local version of

bc.

### Function keywords

**define**

*f*

**(**args

**) {**

Begin the definition of function

fhaving the argumentsargs. The arguments are separated by commas. Statements follow on successive lines. End with}.

**auto**

*x*,

*y*

Set up

xandyas variables local to a function definition, initialized to 0 and meaningless outside the function. Must appear first.

**return(**

*expr*

**)**

Pass the value of expression

exprback to the program. Return 0 if(expr)is left off. Used in function definitions.

**sqrt(**

*expr*

**)**

Compute the square root of expression

expr.

**length(**

*expr*

**)**

Compute how many significant digits are in

expr.

**scale(**

*expr*

**)**

Same as

length, but count only digits to the right of the decimal point.

**read( )**

GNU extension. Read a number from standard input. Return value is the number read, converted via the value of

ibase.

### Math library functions

These are available when **bc** is invoked with **-l**. Library functions set **scale** to 20:

**s(**

*angle*

**)**

Compute the sine of

angle, a constant or expression in radians.

**c(**

*angle*

**)**

Compute the cosine of

angle, a constant or expression in radians.

**a(**

*n*

**)**

Compute the arctangent of

n, returning an angle in radians.

**e(**

*expr*

**)**

Compute

eto the power ofexpr.

**l(**

*expr*

**)**

Compute the natural log of

expr.

**j(**n,

*x*

**)**

Compute the Bessel function of integer order

n.

### Operators

These consist of operators and other symbols. Operators can be arithmetic, unary, assignment, or relational:

**arithmetic**

+-*/%^

**unary**

-++--

**assignment**

=+=-=*=/=%=^=

**relational**

<<=>>== =!=

### Other symbols

**/***

***/**

Enclose comments.

**( )**

Control the evaluation of expressions (change precedence). Can also be used around assignment statements to force the result to print.

**{ }**

Use to group statements.

**[ ]**

Indicate array index.

*text*"

Use as a statement to print

text.

### Examples

Note in these examples

**ibase = 8** **20** **obase = 2** **20** **ibase = A** **scale = 3** **8/7** **obase = 10** **8/7**
1.142

The following lines show the use of functions:

**define p(r,n){** **auto v** **v = r^n** **return(v)}** **scale = 5**
**x = p(2.5,2)** **x** **length(x)** **scale(x)**