This question already has answers here:
Trouble understanding parameter substitution in a script
(1 answer)
Usage of :- (colon dash) in bash
(2 answers)
Closed 9 years ago.
I've found this in a shell script that I use and I'm having trouble finding a formal description/definition of this syntax:
ACTION=${1:-update}
I'm assuming that if $1 variable does not exist (no command line arguments) then "-update" is used.
It's not esoteric. It's POSIX, and even Bourne. In every shell manpage ever. man bash or man ksh. The assumption is mostly right, if the parameter 1 is unset or empty string, then expand the alternate.
http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_02
Related
This question already has answers here:
What is the difference between $(command) and `command` in shell programming?
(6 answers)
What is the benefit of using $() instead of backticks in shell scripts? [duplicate]
(9 answers)
Closed 3 years ago.
In Unix we have 2 ways to execute a command and capture its output in a variable:
1.)
x=`wc-l`
2.)
x=$(wc -l)
Can anyone help me understand the basic difference between the two, and when to use which syntax.
This question already has answers here:
Usage of :- (colon dash) in bash
(2 answers)
Closed 5 years ago.
I encountered ${ID:-} in a shell script, but I am not sure what does this expression exactly do.
After a bit of googling I found that ${var} and $var are both the same. Such expression is useful when we want to expand expression like ${foo}bar.
However I didn't find any source explaining expression like ${ID:-}.
I would like to know about expression ${ID:-}
${variable:-default_value} is used to return the default value, if the variable is not set.
Related: Assigning default values to shell variables with a single command in bash
This question already has answers here:
What does 'cd ${0%/*}' mean in bash?
(2 answers)
What is the meaning of ${0%/*} in a bash script?
(1 answer)
Closed 6 years ago.
I have found this piece of code while studying a bash script:
dir=${0%/*}
I suspect the code inside the braces to be regex but I don't see what it means. Any idea?
It is not a regex, but it is a pattern match. It sets dir to the name of the script, which is $0, but without the last slash and any non-slash after it, if there is a slash in $0. If there is no slash in $0, dir gets a copy of $0 unchanged. See "Parameter Expansion" in the Bash Hackers Wiki.
This question already has answers here:
Usage of :- (colon dash) in bash
(2 answers)
Closed 5 years ago.
Can anyone please tell me what is mean by :- this symbol in Unix shell scripting
while [ ${runq:-$SLOTS} -ge $SLOTS ] in the given example
This is a simple way to provide a default value using an expansion. See http://wiki.bash-hackers.org/syntax/pe#use_a_default_value for more information.
There are also answers to similar questions on stackoverflow like Read a variable in bash with a default value
This question already has answers here:
What are the special dollar sign shell variables?
(4 answers)
Closed 8 years ago.
I am trying to understand a script that was written a few years ago by someone that is no longer available here. The script references $? a few times. What is this?
P.S. Google couldn't help since it seems to strip the $? from the search term
You really should read the GNU bash manual. $? is a special parameter which
expands to the exit status of the most recently executed foreground pipeline.
Read also the advanced bash scripting guide.