LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Quoting brackets inside a bash function (https://www.linuxquestions.org/questions/linux-newbie-8/quoting-brackets-inside-a-bash-function-4175429462/)

sciss 09-28-2012 11:14 AM

Quoting brackets inside a bash function
 
Hey!
I want to use in console a fine calculator Qalculate! It can manage ISU units, brackets - its something like science calculator. I can use it like this:
Code:

$ qalc 2+2
2 + 2 = 4

When I try using brackets, it's gives me back an error:
Code:

$ qalc (2+2)2
bash: błąd składni przy nieoczekiwanym znaczniku `2+2'

(Translation: syntax error near unexpected marker `2+2' )
I can quote it in several ways and then it's work fine:
Code:

$ qalc "(2+2)2"
(2 + 2) * 2 = 8
$ qalc '(2+2)2'
(2 + 2) * 2 = 8
$ qalc \(2+2\)2
(2 + 2) * 2 = 8

But I wanted to use it by alias "c" and without qouting all equation, in.ex.
Code:

$ c (2+2)2
So I wrote a function inside a .bashrc. I tried:
Code:

function c() { qalc $@; }
function c() { qalc "$@"; }
function c() { "qalc $@"; }
function c() { qalc '$@'; }
function c() { qalc \"$@\"; }
function c() { qalc "\"$@\""; }

Nothing works. :( Is there any way to do it?
Sorry for my English.

ted_chou12 09-28-2012 11:34 AM

I don't know the software qalc, but in console, you should be able to do simple arithmetics by default:
Code:

ted:$ echo $((2+2))
4
server:/~
ted:$ echo $((10/2))
5
server:/~
ted:$ echo $((10-2))
8
server:/~
ted:$ echo $((5*(10-2)))
40

If you are trying to achieve something else could you explain a bit more?
Ted

pixellany 09-28-2012 11:56 AM

To multiply, you need "*"----eg:
(2+2)*2

sciss 09-28-2012 11:56 AM

Yes, I'm know that. I use tilda console, and I want to be able to do some quick calculations without problems.
Code:

$ qalc "(54km-456m)/(5h+55s)"
((54 * kilometer) - (456 * meter)) / ((5 * hour) + (55 * second)) = approx. 2.9656051(m / s)

I want the same effect by command (equation without quotation marks and alias qalc=c):
Code:

$ c (54km-456m)/(5h+55s)

sciss 09-28-2012 12:02 PM

Quote:

Originally Posted by pixellany (Post 4791735)
To multiply, you need "*"----eg:
(2+2)*2

Yes, but its not a problem. Qalc understand that:
Code:

$ qalc "(2+2)2"
(2 + 2) * 2 = 8
$ qalc (2+2)2
bash: błąd składni przy nieoczekiwanym znaczniku `2+2'

(syntax error near unexpected marker `2+2' )

ted_chou12 09-28-2012 12:31 PM

can you post the outputs of
Code:

ls /bin | grep qalc
ls /sbin | grep qalc
ls /usr/sbin | grep qalc
ls /usr/bin | grep qalc

separately?
I am trying to get the env here.
Ted

sciss 09-28-2012 12:37 PM

Code:

$ ls /bin | grep qalc
$ ls /sbin | grep qalc
$ ls /usr/sbin | grep qalc
$ ls /usr/bin | grep qalc
qalc
qalculate
qalculate-gtk


ted_chou12 09-28-2012 12:38 PM

do
Code:

ln -s /usr/bin/qalc /usr/bin/c
Now you should be able to do
Code:

c "(2+2)2"
Ted

sciss 09-28-2012 12:47 PM

But it works just fine. I can do
Code:

c "(2+2)2"
But I want to do
Code:

c (2+2)2
Sorry, maybe I wrote it in unnecessarily complicated way.

ted_chou12 09-28-2012 12:51 PM

Can you do
Code:

c \(2+2\)2
?
To my knowledge, () are special characters in bash, without quotation marks, you DO need to escape them, because bash will parse them specially.

sciss 09-28-2012 12:58 PM

yes, I can. I thought it can be done to somehow pass over this rule, and simplify the command.
Thanks for your help.

David the H. 09-30-2012 08:00 AM

Just to make it clear, this simply cannot be done without quoting.

Since the shell parses the line before executing it, and since parentheses and several other characters have special meanings to it, they must be protected from the shell in order for them to be properly passed to the command.

This is true of all commands, BTW. If a raw text string contains anything the shell could consider special, those characters have to be escaped first, either with quotes or backslashes.


You can, however, always execute qalc in interactive mode, where you can talk to it directly without the shell getting in the way.


All times are GMT -5. The time now is 07:49 AM.