Strings & Data Types
Bash is unusual among programming languages: every variable is a string. There are no separate types for numbers or booleans; a value becomes a number only when you use it in an arithmetic context such as $(( )). On top of that, Bash has powerful built-in parameter expansions for slicing and editing strings without calling external tools.
Everything is text
a=10
b=20
echo "$a$b"
echo $(( a + b ))
version="3.9"
echo $(( version + 1 ))1020
30
/w/main.sh: line 6: 3.9: arithmetic syntax error: invalid arithmetic operator (error token is ".9")
String concatenation is just writing values next to each other. The last line fails: $(( )) only knows integers, and 3.9 isn't one.
Types in Bash
| "Type" | How Bash handles it |
|---|---|
| String | The default for every variable |
| Integer | $(( )), (( )), let, or declare -i |
| Decimal | Not supported: use bc or awk |
| Boolean | Exit statuses: 0 = true/success, non-zero = false; or the commands true and false |
| Array | declare -a (indexed) and declare -A (associative), next lesson |
echo "scale=2; 10 / 3" | bc
echo "3.9 + 1.25" | bc
awk 'BEGIN { printf "%.3f\n", 22 / 7 }'
price=19.99; qty=3
echo "total: $(echo "$price * $qty" | bc)"3.33
5.15
3.143
total: 59.97
bc's scale sets the number of decimals for division.
Length, slices and positions
| Expansion | Meaning |
|---|---|
${#s} |
Length of s |
${s:start} |
From position start (0-based) to the end |
${s:start:len} |
len characters from start |
${s: -3} |
The last 3 characters (note the space before -) |
s="Hello, Linux"
echo "length: ${#s}"
echo "${s:0:5}"
echo "${s:7}"
echo "${s: -5}"
echo "${s:7:3}"length: 12
Hello
Linux
Linux
Lin
Removing prefixes and suffixes
These four are used constantly for file names and paths:
| Expansion | Removes from | Match |
|---|---|---|
${s#pattern} |
the start | shortest |
${s##pattern} |
the start | longest |
${s%pattern} |
the end | shortest |
${s%%pattern} |
the end | longest |
path="/var/log/nginx/access.log.gz"
echo "file name: ${path##*/}"
echo "directory: ${path%/*}"
echo "no ext: ${path%.*}"
echo "no exts: ${path%%.*}"
file="${path##*/}"
echo "first ext: ${file#*.}"
echo "last ext: ${file##*.}"file name: access.log.gz
directory: /var/log/nginx
no ext: /var/log/nginx/access.log
no exts: /var/log/nginx/access
first ext: log.gz
last ext: gz
A memory aid: # is to the left of $ on a US keyboard (start), % is to the right (end). Doubling makes it greedy. ${path##*/} is a fast built-in replacement for basename, and ${path%/*} for dirname.
Search and replace
| Expansion | Effect |
|---|---|
${s/old/new} |
Replace the first match |
${s//old/new} |
Replace all matches |
${s/#old/new} |
Replace only at the start |
${s/%old/new} |
Replace only at the end |
${s//old} |
Delete all matches |
msg="the cat sat on the mat"
echo "${msg/at/og}"
echo "${msg//at/og}"
echo "${msg// /_}"
echo "${msg/#the/a}"
csv="a,b,c"
echo "${csv//,/ | }"the cog sat on the mat
the cog sog on the mog
the_cat_sat_on_the_mat
a cat sat on the mat
a | b | c
Changing case
name="ada lovelace"
echo "${name^}"
echo "${name^^}"
LOUD="STOP SHOUTING"
echo "${LOUD,,}"
echo "${LOUD,}"Ada lovelace
ADA LOVELACE
stop shouting
sTOP SHOUTING
^ upper-cases the first character, ^^ all of them; , and ,, lower-case.
Comparing strings
Inside [[ ]], == compares strings and != checks inequality. The right side can be a glob pattern, and =~ matches a regular expression:
file="backup-2026-03-15.tar.gz"
[[ $file == *.tar.gz ]] && echo "a gzipped tarball"
[[ $file == backup-* ]] && echo "a backup file"
if [[ $file =~ ([0-9]{4})-([0-9]{2})-([0-9]{2}) ]]; then
echo "year ${BASH_REMATCH[1]}, month ${BASH_REMATCH[2]}"
fi
[[ -z "" ]] && echo "-z: empty string"
[[ -n "$file" ]] && echo "-n: non-empty string"a gzipped tarball
a backup file
year 2026, month 03
-z: empty string
-n: non-empty string
BASH_REMATCH holds the regex's capture groups. -z tests for an empty string, -n for a non-empty one.
Test yourself
f="photo.backup.jpg". What is ${f%.*}?
- photo
- photo.backup
- jpg
- backup.jpg
Which expansion gives the length of $s?
- ${s.length}
- ${#s}
- $(len s)
- ${s:#}
How do you calculate 7.5 * 2 in a Bash script?
- $(( 7.5 * 2 ))
- echo '7.5 * 2' | bc
- expr 7.5 * 2
- let 7.5*2