`$` symbol use
In Bash scripts, the `$` symbol is used in many different contexts, mainly used to access variable values, parameter expansion, arithmetic operations, and command replacements, etc.:
1. Access the value of the variable
When you prefix the variable name$
When symbols, it will be replaced with the value of the variable.
name="world" echo "Hello, $name"
The output will beHello, world
。
2. Position parameters
$0
Represents the name of the script,$1
arrive$9
Represents the first to ninth parameters,$#
Represents the number of parameters.
echo "Script name: $0" echo "First argument: $1" echo "Number of arguments: $#"
3. Special parameters
$*
and$@
All represent all positional parameters, but are different in quotes.
"$*"
Treat all positional parameters as a single string.
"$@"
Each parameter will be treated as an independent quoted string.
4. Exit status
$?
Represents the exit status (return value) of the previous command. 0 usually means success, other values mean errors or problems.
grep "text" if [ $? -eq 0 ]; then echo "Text found." else echo "Text not found." fi
5. Arithmetic expansion
$((expression))
Used for arithmetic operations.
num1=5 num2=3 sum=$((num1 + num2)) echo $sum # Output 8
6. Command replacement
$(command)
Use command replacement, which executes the command and uses the output as the value of other commands or variables.
files=$(ls) echo "Files in directory: $files"
7. Variable extension in double quotes
When you use a variable in double quotes, the variable expands to its value. Single quotes will not be extended.
greeting="hello" echo "$greeting world" echo '$greeting world'
The first oneecho
The command will be outputhello world
, and the second will output the literal$greeting world
。
Use $ symbol with braces {} in combination
In a Bash script,$
Symbols can be with braces{}
Combination, this combination has several benefits and uses:
1. Variable expansion
Braces are used to clarify the boundaries of variables. This is very useful when variable names may be confused with characters that follow.
greeting="world" echo "Hello, ${greeting}ly" # Will output Hello, worldly
If there are no braces, Bash will try to find the namegreetingly
variable, which is most likely not what you want.
2. String operation
Braces can also be used to perform various string operations on variables, such as extracting substrings, finding and replacing them.
Substring extraction:
str="abcdefghijk" echo "${str:3:4}" # Extract the substring of length 4 from index 3 and output "defg"
default value:
echo "${unset_var:-default_value}" # If the variable unset_var is not set, output "default_value"
replace:
filepath="/path/to/your/file" echo "${filepath/to/from}" # Replace the first matching "to" and output "/path/from/your/file"echo "${filepath//to/from}" # Replace all matching "to" and output "/path/from/your/file"
3. Array operation
Braces are also used to access array elements and perform array operations.
array=(one two three) echo "${array[1]}" # Output "two"
4. Advanced usage of parameter extension
Braces provide many options for advanced parameter extensions, such as string length, pattern matching, etc.
String length:
mystring="hello world" echo "${#mystring}" # Output string length "11"
Mode Delete:
filename="" echo "${filename%.*}" # Delete the shortest match . and its subsequent contents, and output "myfile"echo "${filename%%.*}" # Delete the longest match . and its subsequent contents, and output "myfile"
The use of braces increases the readability and flexibility of the code, which can help you control the behavior and output of variables more accurately.
This is the article about the specific use of $ symbols in bash scripts. For more related bash scripts, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!