0

On mac running 4.4.23(1), an example I've seen used for finding the length of a string throws an error:

string_var=blah
echo `expr length $string_var`
expr: syntax error

Works fine on my Debian system.

shopt options are: himvBHs

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
StevieD
  • 6,925
  • 2
  • 25
  • 45
  • 1
    `expr` isn't POSIX-specified, and isn't part of bash; it's provided by your OS, not the shell. Thus, which features it has available varies between operating systems. – Charles Duffy Sep 20 '18 at 00:26
  • This isn't a duplicate question. The question was "Why doesn't it work on a Mac?" not "how do I get it to work on a Mac?" They are questions of a very different nature. Thanks for the response, though. – StevieD Sep 20 '18 at 03:10
  • "Why" isn't *practical*; it doesn't change how you practice software development. *How* is practical. See the "practical, answerable" criteria ANDed into other bullet points at https://stackoverflow.com/help/on-topic. If I followed your requested interpretation, the question wouldn't be duplicate -- it would be off-topic. – Charles Duffy Sep 20 '18 at 03:38
  • Understanding the software tools you use to develop is extremely practical. From said link: if your question generally covers… a specific programming problem, or a software algorithm, or software tools commonly used by programmers; and is a practical, answerable problem that is unique to software development … then you’re in the right place to ask your question! – StevieD Sep 20 '18 at 03:56
  • Note the **and**. "Practical", like "answerable", has to be true *in addition to* the other criteria. See [What is the rationale for closing "why" questions on language design?](https://meta.stackexchange.com/questions/170394/what-is-the-rationale-for-closing-why-questions-on-a-language-design) -- the consensus outcome of which directly contradicts the claim that questions aimed at understanding reasoning are inherently practical. If questions that had only indirect impact on the practice of programming were on-topic here, our sister site [programmers.se] wouldn't need to exist. – Charles Duffy Sep 20 '18 at 16:07
  • Pertinent also: "You should only ask practical, answerable questions based on actual problems that you face" -- with the related "how" question answered, there *is* no more "actual problem". – Charles Duffy Sep 20 '18 at 16:11
  • 1
    ...that said, to digress into a "how" note for a moment -- in bash, you can test whether something is provided by (and thus part of) your shell (and thus, whether its behavior is going to be tied to your shell version or your OS version) using `type`. `type expr` will typically tell you something like `expr is /usr/bin/expr`, whereas `type type` will tell you `type in a shell builtin`. – Charles Duffy Sep 20 '18 at 16:11

1 Answers1

3

expr is not part of bash -- it's an ancient UNIX tool from back when the shell couldn't do math (or much else useful) on its own.

You don't need it. In the modern day, ${#var} will give you the length of the value assigned to var, as follows:

string_var=blah
echo "${#string_var}"
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441