BASH Programming

What Is the Meaning of $ in Bash Script

Bash is a popular Unix shell that allows users to interact with the system through a command-line interface. In bash scripting, the dollar sign ($) is an essential character used in various ways. The dollar sign has several meanings in bash scripting, and its use is critical to the creation and execution of shell scripts.

What is the Meaning of $ in Bash Scripting

The dollar sign ($) is a special character in bash scripting that is used to represent different meanings depending on the context in which it is used. Here are some of the common uses of the dollar $ sign in bash scripting:

How to Use $ Sign to Retrieve Variable Value in Bash Scripts

In bash scripting, variables are used to store values that can be used throughout the script. The $ symbol is used to denote the value of a variable and below is an example that demonstrate the use of $ Sign to Call Variables in Bash Scripts:

#!/bin/bash

name="Mark"

echo "My name is $name"

In this example, the variable name is assigned the value “Mark”, and the echo statement uses the $ symbol to display the value of the variable, here is the output of this script:

How to Use $ Sign as Positional Parameters

In bash scripting, positional parameters are used to represent arguments passed to a script or function. The $ symbol is used to access the value of positional parameters. Here is an example:

#!/bin/bash

echo "first positional parameter is $1"

echo "second positional parameter is $2"

In this example, the $ symbol is used to display the first and second positional parameters passed to the script and here first argument is “hello”, the second one is Linux:

How to Use $ Sign for Command Substitution in Bash Scripts

In bash scripting, command substitution is used to replace a command with its output. The $ symbol is used to denote command substitution. Here is an example:

#!/bin/bash

today=$(date)

echo "Today is:" $today

In this example, the date command is used to get the current date, and the output is assigned to the variable today using the $ symbol:

How to Use $ Sign With Arithmetic Operations in Bash Scripts

In bash scripting, arithmetic expressions are used to perform mathematical operations. The $ symbol is used to denote arithmetic operation output. Here is an example:

#!/bin/bash

num1=20

num2=30

result=$((num1 + num2))

echo "The result is:" $result

In this example the $ symbol is used to perform arithmetic expansion to add the values of num1 and num2, here I have assigned a value of 20 to num1 and 30 to num2:

How to Use $ Sign With Environment Variables in Bash Scripts

In bash scripting, environment variables are used to store system-wide settings and configurations. The $ symbol is used to access the value of environment variables. Here is an example:

#!/bin/bash

echo "The path is:” $PATH

In this example, the $ symbol is used to display the value of the PATH environment variable:

Conclusion

The dollar sign ($) is a versatile special character in bash scripting that is used for variable and command substitution, representing special variables, and as an escape character. It is different uses make bash scripting more flexible and powerful. There are many uses of this sign, some of them included string manipulation and conditional expressions.

About the author

Aaliyan Javaid

I am an electrical engineer and a technical blogger. My keen interest in embedded systems has led me to write and share my knowledge about them.