echo 'expr 2 +3'
prints expr 2 + 3
(instead of printing 5)
I had read this in book as well as online that within single quotes the expression is evaluated, but on running it is being printed as it is. Is there any dependence on shell . Well, I have tried on Bourne and Korn shell and its not working . Can u Give me some alternative for this and tell me the reason why it is not working?

Dani AI

Generated

The observed result follows from how the shell treats single quotes: everything enclosed in single quotes is taken literally, so no parameter expansion, command substitution, or arithmetic is performed. That explains 's output and matches 's shorthand “because it shouldn't.” See the Bash manual for the quoting rules for authoritative detail: Quoting.

To actually evaluate an expression there are two common approaches. correctly pointed to command substitution (the shell runs a command and replaces it with the command's output). A cleaner, built‑in alternative in modern POSIX shells and in bash/ksh is arithmetic expansion; for example:

echo $((2 + 3))

Arithmetic expansion runs inside the shell (no external program) and is simpler and faster for numeric calculations. See the Bash documentation on arithmetic expansion: Arithmetic Expansion.

A few troubleshooting and portability notes: the legacy external utility expr is standardized by POSIX and treats operators as separate arguments, so operators usually need surrounding spaces or escaping — consult the POSIX page for details: expr. If code must run on very old Bourne shells that lack arithmetic expansion, use expr combined with command substitution. Otherwise prefer shell arithmetic for clarity and performance.

Recommended Answers

All 3 Replies

You're getting the quote character confused with the backtick character. If you run

echo `expr 2 + 3`

you'll get the expected output.

Alternatively, you can also use $(command) as well, which has the same effect:

echo $(expr 2 + 3)

echo ' expr 2 + 3' is not working. i cannot figure out why?? Well thanks for the alternative answer. It worked out.

why? because it shouldn't.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.