Linux command
dollar 命令
文本
复制后可按需替换文件名、目录或参数。
常用示例
Reference a variable
echo $[VARIABLE]
Command substitution
result=$(command)
Arithmetic expansion
echo $((1 + 2))
Parameter expansion with default
echo ${VAR:-[default]}
String length
echo ${#VAR}
说明
$ is the shell's expansion operator for variables, commands, and arithmetic. It triggers substitution of values before command execution. Variable expansion $VAR or ${VAR} retrieves the variable's value. Braces allow modifiers and are required for array access and complex expansions. Command substitution $(command) executes the command and substitutes its output. This replaces the older backtick syntax. Arithmetic expansion $((expr)) evaluates mathematical expressions.
参数
- ${var}
- Value of variable.
- ${var:-default}
- Use default if var is unset or empty.
- ${var:=default}
- Assign default if var is unset or empty.
- ${var:+alternate}
- Use alternate value if var is set.
- ${var:?error}
- Exit with error message if var is unset.
- ${#var}
- String length of var.
- ${var%pattern}
- Remove shortest suffix match.
- ${var%%pattern}
- Remove longest suffix match.
- ${var#pattern}
- Remove shortest prefix match.
- ${var##pattern}
- Remove longest prefix match.
- ${var/old/new}
- Replace first occurrence of old with new.
- ${var//old/new}
- Replace all occurrences of old with new.
- ${var:offset:length}
- Substring extraction.
- $?
- Exit status of last command.
- $$
- PID of the current shell.
- $!
- PID of last background job.
- $0
- Script or shell name.
- $1-$9
- Positional parameters.
- $@
- All positional parameters as separate words.
- $*
- All positional parameters as a single word.
- $#
- Number of positional parameters.
FAQ
What is the dollar command used for?
$ is the shell's expansion operator for variables, commands, and arithmetic. It triggers substitution of values before command execution. Variable expansion $VAR or ${VAR} retrieves the variable's value. Braces allow modifiers and are required for array access and complex expansions. Command substitution $(command) executes the command and substitutes its output. This replaces the older backtick syntax. Arithmetic expansion $((expr)) evaluates mathematical expressions.
How do I run a basic dollar example?
Run `echo $[VARIABLE]` in a terminal, then adjust file names, paths, flags, or remote targets for your system.
What does ${var} do in dollar?
Value of variable.