0

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.

  • 1
    It is totally non-sense I believe – Derviş Kayımbaşıoğlu Jan 15 '19 at 22:58
  • This is an error in my bash. If you are getting output, please check that what you posted is actually what you tried (you also have a different code in title and in body of your post). Some further context might also help, if available. – Amadan Jan 15 '19 at 23:00
  • Regardless of whether it's nonsense, it's certainly code nobody should ever use. Bash's built-in facilities are far superior to `expr`, which is an antique from the 1970s present only for backwards compatibility. – Charles Duffy Jan 15 '19 at 23:07
  • As it's in single quotes it's literally the string "${ -b :board}" and the first three characters are "${ ". Append that to "yahoo" and you get "yahoo${ ". – sticky bit Jan 15 '19 at 23:07
  • Note also that we generally don't permit "explain this code" questions here. See [How to handle “Explain how this ${code dump} works” questions](https://meta.stackoverflow.com/questions/253894/how-to-handle-explain-how-this-code-dump-works-questions) on [meta]. – Charles Duffy Jan 15 '19 at 23:08
  • Sorry about posting on the wrong topic. – pranmar123 Jan 15 '19 at 23:19
  • Out of curiosity, where does this code come from? It almost looks like it's code that is generated by other code. – Benjamin W. Jan 16 '19 at 16:40

1 Answers1

1

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 of string beginning at position with length at most length. If either position or length 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.

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
  • Thank you for the explanation. I have one more question... Another line goes like this: word=$word"${ -s :psize}" How would this line work then? – pranmar123 Jan 15 '19 at 23:26
  • @PranayMarella That one makes even less sense than the original (and gives a "bad substitution" error in bash). Where on earth are you getting this? – Gordon Davisson Jan 16 '19 at 01:52
  • Note that `expr` is part of the POSIX standard, and the Coreutils package just provides a particular implementation of `expr`. The `substr` command, though, *is* a GNU extension to `expr`. – chepner Jan 23 '19 at 15:49
  • @chepner Good point; `substr`, in particular, doesn't seem to be part of POSIX `expr` (using it as a string argument produces unspecified results). BSD `expr` treats it as a normal string. – Benjamin W. Jan 23 '19 at 15:53