1. Introduction

Prefixing is the idea of appending a special symbol before a variable or Bash statement to change how it functions. For example, user-defined or built-in variables in Bash can be referenced using the $ sign, irrespective of their type.

In this tutorial, we’ll explore the purpose of prefixing a string with a $ sign by explaining different scenarios based on practical examples.

2. Purpose of Prefixing a String With The $ Sign

Sometimes, we must prefix strings or expressions with the $ sign in Bash. For example, we can’t print the value of variables in Bash without adding a $ sign as a prefix. Consequently, the goal of prefixing Bash statements with the $ sign is to carry out special functions and execute powerful Bash commands.

Next, we’ll cover several use cases of the $ sign prefix in Bash.

3. Printing the Value of a Variable

In Bash, we can use the $ sign as a prefix to represent the value of a variable. The echo command can be used to output the values of built-in and user-defined variables.

For instance, we initialize a variable v with the string Hello. We then execute the echo statement without prefixing v with the $ sign to examine the output:

$ v="Hello"
$ echo v
v

In this case, the echo command outputs v, treating it as a string value rather than a variable.

Let’s try the echo command by prefixing the v variable with the $ sign. Notably, we shouldn’t add a space between the $ symbol and the v variable*:*

$ echo $v
Hello

The output now displays the value of the variable v, which is the string Hello.

Now, let’s try the same command but with a space between the $ sign and the variable:

echo $ v
$ v

This time, the echo statement treats the $ sign and the v variable as separate string values. This illustrates the importance of exercising caution when using special characters in Bash.

Similarly, a Bash variable can be incorporated with a string using the echo statement to get a concatenated result.

For instance, we initialize a variable v with the value I am good. Then, we use an echo statement to print the string Hello followed by the value of the variable v. The result displays the concatenated value of Hello and the v variable as Hello I am good:

$ v="I am good"
$ echo Hello $v
Hello I am good

Like user-defined variables, the echo statement can display system variables in Bash. PWD is an environment variable that stores the path of the current working directory. Using echo, we can print the value of this system variable by prefixing PWD with the $ sign:

$ echo $PWD
/home/aqsa

This shows the path of the current working directory as /home/aqsa.

4. Prefixing a String With $ Sign Within Single Quotes

Single quotes avoid word splitting of a string in Bash while used within the echo command. Let’s run an echo statement by using a string in single quotes.

$ echo 'Hello to new learning'
Hello to new learning

The output displays the original string verbatim.

However, working with strings that include several placeholders is fairly typical in modern computing. User-defined or built-in variables can be used as placeholders. We usually refer to this procedure as variable expansion or string interpolation.

During this process, we substitute the initial values of particular placeholders into the output. However, variable interpolation isn’t possible with expressions containing single quotes in Bash.

Here, we’re initializing a variable v with the string value bash. Then, the variable v is prefixed using the $ sign:

$ v="bash"
$ echo 'Hello to $v'
Hello to $v

As expected, the output prints $v instead of its original value, bash, due to the use of single quotes*,* and the prefixed $ sign doesn’t perform the interpolation.

5. Prefixing a String With $ Sign Within Double Quotes

Like single quotes, using double quotes avoids word splitting for any string used within the double quotes of an echo statement.

We can do variable interpolation by incorporating the $ sign into double quotes; that is, we can utilize the variable prefixed with the $ sign as a fused parameter.

Let’s initialize a variable v with the value New. The $ sign in the double-quoted string inside an echo command is used to reference the value of the v variable. As a result, the v variable is expanded within the double quotes, and the concatenated string, New World, is printed*:*

$ v="New"
$ echo $v
New
$ echo "$v World"
New World

Notably, if we prefix the $ sign outside the double quotes instead of prefixing it to the v variable, no variable expansion is carried out.

The echo statement treats the v variable as a string:

$ echo $"v World"
v World

With the $ sign prefix, Bash permits more than one variable to expand inside double quotes. To confirm this, we run an echo statement with a user-defined variable, v, and a system variable, HOME, enclosed in double quotes:

$ echo "${v} $HOME"
New /home/aqsa

The output of the echo statement shows the value New for the v variable and /home/aqsa for the HOME variable.

6. Interpreting Escape Sequences

The expected outcome for escape sequences such as newline (\n) and tab (\t) characters in an echo statement might not be obtained if the expression isn’t properly prefixed with the $ sign. The $’…’ syntax is known as ANSI-C quoting.

First, we use an echo statement with single quotes containing newline characters (\n) with the intent of adding a line after each word:

$ echo 'New\n World\n'
New\n World\n

However, we haven’t prefixed the entire string with a $ sign. Therefore, the echo statement cannot interpret the newline character and treats it as a normal string.

Next, let’s prefix the whole string with a $ sign:

$ echo $'New\n World\n'
New
 World

This time, the echo statement successfully interprets the newline characters and adds a line break after each word.

7. Using $((…)) to Perform Arithmetic Operations

In Bash, we can use the $((…)) syntax to perform arithmetic operations like addition or subtraction.

For instance, we initialize two variables, v1 and v2, with the values 5 and 6. The variable r is defined as the sum of variables v1 and v2 within double parentheses, while the $ sign is used as a prefix to the expression. The echo statement then prints the value of the r variable as output, which is 11:

$ v1=5
$ v2=6
$ r=$((v1 + v2))
$ echo $r
11

Notably, we can’t perform an arithmetic operation between a string and an integer variable. Let’s try this by replacing the value of v2 with the string Hello and perform the same addition operation*:*

$ v1=5
$ v2="Hello"
$ r=$((v1 + v2))
$ echo $r
bash: line 3: ((: 5 + Hello: syntax error: invalid arithmetic operator (error token is "Hello")

We’ll get a syntax error, as expected.

8. Conclusion

In this article, we discussed common and fundamental uses for prefixing a string with the $ sign in Bash programming. We can access the values of various environment variables, user-defined variables, and strings by using the $ sign as a prefix. Moreover, prefixing a single-quoted string with the $ sign enables interpreting various escape sequences such as newline and tab characters. Lastly, using the $((…)) syntax can enable some of the most commonly used arithmetic operations in Bash.