I am working on some bash scripting conversion to C++ and came across this line...
word="yahoo"
word=$word"`expr substr '${ -b :board}' 1 3`"
I understand what expr substr does, but the argument that I provide "${ -b :board}" does not make any sense to me.
When I tried to run that on terminal:
echo $word
Output:
yahoo${
I would appreciate any input, thanks.
This question is not about Bash or sh, but about expr
, a standalone command that is part of, e.g., the GNU Coreutils. If we consult the manual, we find
expr
evaluates an expression and writes the result on standard output. Each token of the expression must be a separate argument.
and
substr string position length
Returns the substring ofstring
beginning atposition
with length at mostlength
. If eitherposition
orlength
is negative, zero, or non-numeric, returns the null string.
So the command
expr substr '${ -b :board}' 1 3
takes the string ${ -b :board}
and extracts a substring of length 3, starting at position 1, which is ${
.
The command
word=$word"`expr substr '${ -b :board}' 1 3`"
puts the expr
command into a command substitution (the backticks) and appends the result to the expansion of $word
, which at this point contains yahoo
, so that's how you end up with yahoo${
.
This all being said, I don't see the reason to do it like that. The output of the expr
command is a constant string, so you could really just replace everything with
word='yahoo${ '
As a side note, in modern Bash, you could get the same functionality with parameter expansion:
word='yahoo'
var='${ -b :board}'
word+=${var:0:3}
But the result is the same, and without more context seems to not make any sense in the first place.