Usage of $ sign in Bash Scripting
Introduction
Bash scripting is a powerful tool that allows Linux users to automate repetitive tasks, perform system maintenance, and much more. One of the most important features of Bash scripting is the ability to use variables. A variable is a container that holds a value, and it can be used to store text, numbers, or even command output. In Bash scripting, the $ sign is used to indicate that a variable is being used.
Example
$0
$$
$!
$#
$?
${10}
$*
$@
${array[i]}
${#array[@]}
${#array[i]}
Explanation
#!/usr/bin/env bash
echo "The program $0 is now running"
echo "The progress PID is $$"
echo "The PID of the last executed command is $!"
echo "The number of parameter is $# "
echo "The return code of last command is $?"
echo "The first parameter is $1"
echo "The second parameter is $2"
echo "The tenth parameter is ${10}"
echo "The parameters are: $*"
echo "Again, the parameters are: $@"
echo ${array[i]} # displays array's value for this index. If no index is supplied, array element 0 is assumed
echo ${#array[@]} # to find out the length of any element in the array
echo ${#array[i]} # to find out how many values there are in the array
Conclusion
In Bash scripting, the $ sign is used to indicate that a variable is being used. It can be used to access user-defined variables, the output of commands, and special variables. By mastering the usage of the $ sign, Bash scripting becomes a powerful tool for Linux users to automate tasks and improve their productivity.
Leave a message
Disclaimer
- Welcome to visit the knowledge base of SRE and DevOps!
- License under CC BY-NC 4.0
- Made with Material for MkDocs and improve writing by generative AI tools
- Copyright issue feedback me#imzye.com, replace # with @