JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM >

Variable interpolation in Bash scripts

Author:JIYIK Last Updated:2025/03/23 Views:

This article is about using variables in Bash scripts and how to insert these variables in Bash scripts.


Variables in Bash Scripts

Every programming language has variables with specific data types. Similarly, Bash script also allows us to use variables to store our data values.

Unlike other programming languages, Bash does not restrict the type of data we can have in a variable. Bash variables can contain strings, numbers, characters, or anything you want.

Furthermore, we do not need to declare a variable to use it. It is sufficient to simply assign a value to it before referencing it.

Let's look at a simple example of creating and using a variable:

#!/bin/bash
var="Hello World"
echo $var

This script creates the var variable and stores it as a string value. Later, in the next line, we print the value of this variable.

Note that to reference the variable, we use the $ sign to replace the value when the script is executed.

Creating and using variables


Variable interpolation in Bash scripts

You often need to use a variable value and concatenate it with another text or number. To do this, we need curly braces to reference the variable.

The question is, where to use curly braces {}and where to use parentheses (). Let's see the difference between the two.

Use of curly braces {}

Curly braces are called parameter expansion. We use curly braces when we need to print characters other than space after the variable value.

We then place the variables {}in curly braces, like this:

#!/bin/bash
var="Hello"
echo ${var}World

请注意, in the above script, we created a variable varand stored Hello. In the previous example, we did not use curly braces to refer to it because we did not need to add another word with it.

If we leave out the curly braces now, it will search for a variable called varWorld, but it won't find one. So, to tell the script the exact variable name, we enclose it in curly braces.

Its output is as follows:

Bash variable interpolation - use of curly braces

Use of parentheses ()

The parentheses are called command expansion. Command substitution allows the output of a command to replace the command itself.

After removing the trailing newline, Bash executes command and replaces the command substitution with the standard output of command. Embedded newlines are not removed; however, they may be removed during word splitting.

Command substitution occurs when you include a command, as follows:

#!/bin/bash
day = $(date)
echo "Today is ${day}"

In the above script, date is a command which tells the current system date and time. So, when the script is executed, date will be replaced with the output of the command and assigned to the variable day.

The echo command prints the following lines:

Bash variable interpolation - use of parentheses

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Hosting Docker Internal in Linux

Publish Date:2025/03/23 Views:143 Category:OPERATING SYSTEM

Docker allows developers to efficiently build, test, and deploy applications by packaging them in standardized units called containers. When working with Docker containers, you may encounter scenarios where you need to connect a container t

Setting the working directory in Docker

Publish Date:2025/03/23 Views:198 Category:OPERATING SYSTEM

If present, the working directory of a process in Compute is a directory in a linked hierarchical file system that is dynamic for each process. In Docker, we can set our working directory by editing the Dockerfile and adding the key WORKDIR

How to get IP address in CentOS

Publish Date:2025/03/23 Views:108 Category:OPERATING SYSTEM

This short article is a brief introduction to CentOS followed by a brief discussion on how we can get the server IP address in CentOS using the Command Line Interface (CLI). This article will discuss some of the commands and their usage for

Updating YUM in Linux

Publish Date:2025/03/23 Views:148 Category:OPERATING SYSTEM

This article will teach us how to update YUM in Linux and how to install, update, remove, find and manage packages on a Linux system. We have also seen the difference yum update between and in Linux yum upgrade . yum update command in Linux

Installing Deb Files in Linux

Publish Date:2025/03/23 Views:93 Category:OPERATING SYSTEM

In this Linux article, we will learn how to install .deb (Debian Package) files on Linux systems. We will also see how to remove .deb files after installation. More importantly, we will learn different ways to install .deb files on Linux sy

lsof Command in Linux

Publish Date:2025/03/23 Views:100 Category:OPERATING SYSTEM

In this Linux article, we will learn about lsof command in Linux operating system. We will see how to use this command for different purposes in Linux. We use lsof the lsof command to verify the ports in use on the Linux operating system. U

ps aux command in Linux

Publish Date:2025/03/23 Views:57 Category:OPERATING SYSTEM

If you are using Linux and are looking for a tool that can monitor all the processes running on your system, then you should use the command ps aux. This command will show you an overview of all running processes. It is very useful for trou

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial