Search⌘ K
AI Features

Bash variables and functions

Explore the concept of Bash variables and functions, including setting and reading variables, different variable types, and using functions to organize code in shell scripts. Understand how to declare local and global variables and apply these concepts to create practical scripts.

We'll cover the following...

A variable is a temporary store for a piece of information. There are two actions we may perform for variables: first - setting a value for a variable, second - reading the value for a variable. To read the variable we place its name preceded by a $ sign. To learn more, let’s create a shell script that will backup the home directory into a zipped (tar.gz) file.

Shell
#!/bin/bash
filename=homedirbackup_$(date +%Y%m%d).tar.gz
tar -czf $filename /$home

Here the variable filename ...