cnd
cnd

Reputation: 33784

How to get quoted arguments in the correct way?

I've got function

calc() {echo "${1}"|bc -l;}

it works for 2+2 but when I want something alike 10^4

calc 10^4
zsh: no matches found: 10^4

yes I'm getting the same with bc -l

>>echo 10^4|bc -l
zsh: no matches found: 10^4

but to solve it I've added quotes

>>echo "10^4"|bc -l
10000

how to implement it in function? if I do "\"${1}\"" it will just echo the string...

Upvotes: 1

Views: 63

Answers (1)

Anton Kovalenko
Anton Kovalenko

Reputation: 21517

^ is a special character when EXTENDED_GLOB option is enabled in zsh. It's expanded before your function is called, so there's no workaround possible inside the function.

You can disable EXTENDED_GLOB altogether:

setopt no_extended_glob

or provide an alias for interactive use, which would expand into noglob calc, preventing filename expansion:

alias calc='noglob calc'

Upvotes: 3

Related Questions