Skip to content
Home » Bash Variables 101: A Beginner’s Guide to Working with Arguments, Strings and Integers

Bash Variables 101: A Beginner’s Guide to Working with Arguments, Strings and Integers

Welcome to this blog on bash variables! In this post, we will discuss various types of variables that can be used in bash scripts, as well as some examples of how they can be used to increase productivity and efficiency.

Variables with arguments

First, let’s start with bash variables that can be passed as arguments to a script. For example, consider the following script called print_url.sh:

#!/bin/bash
echo "The URL is: $1"

If we run this script with the argument itvraag.nl, the output would be:

$ bash print_url.sh itvraag.nl
The URL is: itvraag.nl

In this example, the variable $1 represents the first argument passed to the script. Similarly, we can use $2 for the second argument, $3 for the third, and so on.

Comments

Next, let’s look at comments in bash scripts. Comments are lines of code that are ignored by the interpreter and are used to provide explanations or notes about the code. In bash, comments are denoted by a # symbol at the beginning of the line. For example:

# This is a comment
echo "Hello, World!" # This is also a comment

In the above example, the first line is a comment, and the second line is a command followed by a comment.

String

Now let’s move on to strings and integers in bash. A string is a sequence of characters, and in bash, we can define a string variable by enclosing it in quotes. For example:

# Define a string variable
name="itvraag.nl"

# Print the string variable
echo "The name is $name"

The output of the above code would be:

The name is itvraag.nl

Integer

On the other hand, an integer is a whole number and can be defined in bash using the declare command. For example:

# Define an integer variable
declare -i num=10

# Print the integer variable
echo "The number is $num"

The output of the above code would be:

The number is 10

System variables

In bash, we can also use system variables, which are predefined variables that are set by the system or the interpreter. Some examples of system variables include $HOME, which represents the home directory of the current user, and $PATH, which is a list of directories that the shell searches for commands.

One thing that many people may not know about bash variables is that they can be used in arithmetic operations. For example, we can use the expr command to perform arithmetic operations on variables:

# Define two integer variables
declare -i num1=10
declare -i num2=20

# Perform arithmetic operations on variables
result=`expr $num1 + $num2`
echo "The result is $result"

The output of the above code would be:

The result is 30

Productivity

Now let’s look at some examples of how we can use bash variables to increase productivity and efficiency.

  1. Use variables to pass arguments to scripts: As we saw in the first example, we can use variables to pass arguments to scripts, which can make it easier to reuse the script and customize its behavior.
  2. Use variables to store frequently used values: Instead of hardcoding values that are used frequently in your scripts, you can store them in variables and use the variables instead. This can save time and make it easier to update the values if needed.
  3. Use variables in arithmetic operations: As we saw in the previous example, we can use variables in arithmetic operations, which can make it easier to perform calculations in our scripts.
  4. Use variables to store the output of commands: Instead of running the same command multiple times, we can store the output in a variable and use the variable as needed. This can save time and make the script more efficient.
  5. Use variables to customize the behavior of scripts: By using variables to store different values and using them to control the behavior of the script, we can make our scripts more flexible and adaptable to different situations.

Challenge

To test what we have discussed in this blog, try the following challenge:

  • Write a bash script that takes two arguments (strings) and concatenates them together. For example, if the script is called concatenate.sh and the arguments are Hello and World, the output should be Hello World.
  • Use a variable to store the concatenated string and print it to the screen.
  • Use the declare command to define an integer variable and perform some arithmetic operations on it.
  • Use a system variable (e.g. $HOME) in the script.

Leave a Reply

Your email address will not be published. Required fields are marked *

3 × 3 =