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

Bash Cheatsheet

This document discusses different types of shell parameters and expansions. There are three types of parameters: positional parameters, variables, and special parameters. Variables can be user-defined or shell variables and are set using name=value. Common shell variables include HOME, PATH, USER, and HOSTNAME. Parameter expansion retrieves the value of a parameter using $parameter or ${parameter}. Arithmetic expansion performs mathematical calculations using $((expression)). The bc command can also be used for arithmetic.

Uploaded by

Dick Go
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Bash Cheatsheet

This document discusses different types of shell parameters and expansions. There are three types of parameters: positional parameters, variables, and special parameters. Variables can be user-defined or shell variables and are set using name=value. Common shell variables include HOME, PATH, USER, and HOSTNAME. Parameter expansion retrieves the value of a parameter using $parameter or ${parameter}. Arithmetic expansion performs mathematical calculations using $((expression)). The bc command can also be used for arithmetic.

Uploaded by

Dick Go
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

SECTION 2

VARIABLES AND
SHELL EXPANSIONS

SECTION CHEAT SHEET


PARAMETERS

THERE ARE 3 TYPES OF PARAMETERS

POSITIONAL SPECIAL
VARIABLES PARAMETERS PARAMETERS

VARIABLES

DEFINITION :
Variables are parameters that you can change the value of

2 TYPES OF VARIABLES

1 USER-DEFINED
VARIABLES

BOURNE SHELL VARIABLES

2 SHELL
VARIABLES
BASH SHELL VARIABLES
SETTING THE VALUE OF A VARIABLE
name=value

Note 1: There should be no spaces around the equals sign


Note 2: Names of user-defined variables should be all lowercase

SOME COMMON SHELL VARIABLES

HOME Absolute path to the current user's home directory

PATH List of directories that the shell should search for executable files

USER The current user's username

HOSTNAME The name of the current machine

HOSTTYPE The current machine's CPU architecture

PS1 The terminal prompt string

Link to a list of Bourne shell variables

Link to a list of Bash shell variables


PARAMETER EXPANSION
DEFINITION:
Parameter expansion is used to retrieve the value stored in
a parameter

SYNTAX:
Simple Syntax: $parameter
Advanced Syntax: ${parameter}

PARAMETER EXPANSION TRICKS


1 ${parameter^}
Convert the first character of the parameter to uppercase

2 ${parameter^^}
Convert all characters of the parameter to uppercase

3 ${parameter,}
Convert the first character of the parameter to lowercase

4 ${parameter,,}
Convert all characters of the parameter to lowercase

5 ${#parameter}

Display how many characters the variable’s value contains

6 ${parameter : offset : length}


The shell will expand the value of the parameter starting at
character number defined by “offset” and expand up to a
length of “length”

Note: None of these alter the value stored in the parameter. They
just change how it is displayed after the expansion.

Link to list of more parameter expansion tricks


COMMAND SUBSTITUTION

DEFINITION :
Command Substitution is used to directly reference the
result of a command

Syntax for command substitution

$(command)

 ARITHMETIC EXPANSION

DEFINITION :
Arithmetic Expansion is used to perform mathematical
calculations in your scripts.

Syntax for Arithmetic Expansion

$(( expression ))
ARITHMETIC OPERATORS RANKED IN ORDER OF
PRECEDENCE (HIGHEST PRECEDENCE FIRST):

OPERATOR(S) MEANING(S) COMMENTS

Anything placed in parentheses is given


( ) Parentheses
the highest precedence and is always
run first.

Exponentiation.
**
2**4 means 2 to the power of
4, which is 16

*, /, and % Multiplication, Division, and These have the same precedence.


Modulo.

Modulo calculates the


remainder of a division.

Addition and substraction These have the same precedence.


+ and -

Note: When two operators have the same precedence, the one furthest to the
left gets performed first.

THE BC COMMAND

Using the bc command

echo “expression” | bc

Using the scale variable to control the number decimal places shown

echo “scale=value; expression” | bc

You might also like