Sage-Code Laboratory
index<--

Bash Data Types

Bash is a dynamic language that do not use data types to diferentiate variables. Basicly all variables are strings but depending on the context, Bash permits arithmetic operations and comparisons on variables.

Declare variables

Bash enable you to create variables and set initial values. Later you can extract or modify the value of a variable using specific operators and expressions or commands. The most simple way to define a variable is to assign initial value to an identifier that becomes the variable name.

Identifier Name

Identifier name can't start with a number or special character. It usually start with a letter and can be followed by more letters, digits or underscore. You can't use a dash or a space for identifiers. Identifiers are case sensitive.

Initial Value

Initial value is given by using assign sign "=". Some other operators like "+=" for examples will cause a new variable to be created and assigned the default zero value. This can be an empty string or 0 depending on the context.

Console
#!/bin/bash
var=1
echo $var

str="hello world"
echo $str 

new+=1
echo $new

Constant Literals

Bash has support for data literals. Numeric data literals are by defaul parsed as decimal integers. If a literal look like a number it can be parsed and interpreted as a number depending on the context.

A sequence of symbols that are not separated by paranthesis or spaces can be considered string even if is not enclosed in double quotes. That may be confusing, therefore is a good practice in Bash to use double quotes to contain a string.

Example

In next example we display a sequence of commands given to the Bash interpreter in CLI (Command Line Interface). You can do this for quick testing or issuing commands.

Console
>bash
bash-3.2$
bash-3.2$ xp=1+2+3
bash-3.2$ echo $xp 
1+2+3
bash-3.2$

Note: In previous example we may expect for the expresion to be evaluated and the result to be numeric but Bash has other ideas. It will create a string that is containing the expression as given.

Quoted Strings

Quoted strings can support spaces and paranthesis. Also, there is an escape convention. A string can contain special characters escaped like in C using backslash symbol: "\"

Examples

Console
~>bash
$ echo "This is \n a long string \n with new lines"
This is
a long string
with new lines
$exit
~>

Note: In this example, $ is the Bash prompt. Bash is waiting for a new command. You can stop CLI by typing command: exit. Symbol ~ represent home folder and is sometimes present in the command line prompt.

Empty variables

You can't test directly for type of a variable but you can test if a variable is empty using a conditional expression. We have not yet learn how to make conditionals.

Examples

Expression [ -z "$var" ] represents a conditional expression that is evaluated to true if variable var is empty or unset. You will understand this example later after you learn conditionals and logical expressions.

Console
>bash
$ [ -z "$var" ] && echo "var is undefined"
$ var is undefined
$ var=0
$ [[! -z "$var" ]] && echo "var is defined"
var is defined
$

Environment Variables

Every Bash session has a global scope. In this scope you can define environment variables. This is an array that contains name=value pairs. There are specific commands to add variables to this environment and inspect values. Usually these variables are defined using capitall letters.

You can investigate the environment variales using command "env" or "set". To extract value of a single variable you can use command "echo" and prefix variable name with symbol "$". This may be confusing for beginners, because the Bash prompt uses the same symbol. Do not confuse the two.

You can add a new variable to the environment by using command "export". After export the variable is available to echo command so you can display it's value. This command can also export functions not only variables!

Console
>bash
$ export TEST="bash is cool" 
$ echo $TEST
$ bash is cool
$ 

String manipulation

Strings can be concatenated in Bash if they are side by side with other strings but not separated by a space. Any adiacent symbol except parathesis and spaces will be concatenated to the string. Strings can be modified using "+=" operator.

For instance:

Console
>bash
$ echo "hello ""world" 
hello world
$ echo a" test"
a test
$ 

String interpolation

Strings can be combined with variables. For this we use symbol "$" in front of variable, to access its value. The value is inserted in the string, replacing the variable placeholder. The string formating works with command "echo" and "printf" but also in expressions.

For instance:

Console
>bash
  $ str="world"
  $ echo "hello &str" 
  hello world
  $ x = 10 
  $ echo "x = $x"
  x = 10
  $ 

Longer strings

Long strings are possible on multiple lines. The spaces in long strings are preserved as is. So you can write a sequence of code and the indentation is preserved. You can also use the end of line continuation inside a string but is not necesary. The continuation symbol is backslash. "\"

Example

I have create this example named "longstr.sh" on replit.com website.

longstr.sh
#!/bin/bash
echo \
"this
   is
   a
   long
   string"
Console
>bash longstr.sh
this
  is
  a
  long
  string
$

Read next: Arithmetic Expressions