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

How to decompress x.tar.xz format files under Linux

Publish Date:2025/04/08 Views:186 Category:OPERATING SYSTEM

A lot of software found today is in the tar.xz format, which is a lossless data compression file format that uses the LZMA compression algorithm. Like gzip and bzip2, it supports multiple file compression, but the convention is not to compr

Summary of vim common commands

Publish Date:2025/04/08 Views:115 Category:OPERATING SYSTEM

In Linux, the best editor should be vim. However, the complex commands behind vim's powerful functions also make us daunted. Of course, these commands do not need to be memorized by rote. As long as you practice using vim more, you can reme

Detailed explanation of command return value $? in Linux

Publish Date:2025/04/08 Views:58 Category:OPERATING SYSTEM

? is a special variable. This variable represents the return value of the previous command. That is to say, when we run certain commands, these commands will return a code after running. Generally, if the command is successfully run, the re

Common judgment formulas for Linux script shell

Publish Date:2025/04/08 Views:159 Category:OPERATING SYSTEM

In shell script programming, predicates are often used. There are two ways to use predicates, one is to use test, and the other is to use []. Let's take a look at how to use these two methods through two simple examples. Example 1 # test –

How to use the Linux file remote copy command scp

Publish Date:2025/04/08 Views:151 Category:OPERATING SYSTEM

Scp copies files between two hosts over the network, and the data is encrypted during transmission. Its underlying layer uses ssh for data transmission. And it has the same authentication mechanism and the same security level as ssh. When u

Scan to Read All Tech Tutorials

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

Recommended

Tags

Scan the Code
Easier Access Tutorial