LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-26-2021, 03:09 PM   #1
bsmile
Member
 
Registered: Oct 2017
Posts: 98

Rep: Reputation: Disabled
how to let script recognize parenthesis input on the command line?


I am writing a simple script to let bc evaluate simple math expressions and output on the screen. I write down a file called mybc as

Code:
#!/bin/sh

express=$1

echo "scale=6; $express" | bc
and run it on the command line as

mybc sqrt(6.0)

and get the following error message

bash: syntax error near unexpected token `('

If I run the following on the command line as

mybc sqrt\(6.0\)

It can work correctly. But I hope mybc can just run as the first way. I searched a bit and landed on the following link

https://stackoverflow.com/questions/...-the-command-l

but it seems to help with parenthesis used within the script, and thus does not help.
 
Old 04-26-2021, 03:50 PM   #2
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 3,347

Rep: Reputation: Disabled
The "problem" is not with the script but with the command interpreter.

bash interprets parentheses as instructions to run commands in a subshell. There's nothing your script can do to prevent this.
 
Old 04-26-2021, 07:58 PM   #3
JeremyBoden
Senior Member
 
Registered: Nov 2011
Location: London, UK
Distribution: Debian
Posts: 1,947

Rep: Reputation: 511Reputation: 511Reputation: 511Reputation: 511Reputation: 511Reputation: 511
Unless you use quotes:-
Code:
#!/bin/bash
echo "scale=6; $1" | bc
Code:
./mybc "sqrt(6.0)"
2.449489
 
Old 04-29-2021, 12:25 AM   #4
bsmile
Member
 
Registered: Oct 2017
Posts: 98

Original Poster
Rep: Reputation: Disabled
Thanks! I think this works. I can give every expression a quotation mark, a bit more convenient than giving a back slash on each parenthesis.

Quote:
Originally Posted by JeremyBoden View Post
Unless you use quotes:-
Code:
#!/bin/bash
echo "scale=6; $1" | bc
Code:
./mybc "sqrt(6.0)"
2.449489
 
Old 04-29-2021, 07:06 AM   #5
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,628

Rep: Reputation: 2557Reputation: 2557Reputation: 2557Reputation: 2557Reputation: 2557Reputation: 2557Reputation: 2557Reputation: 2557Reputation: 2557Reputation: 2557Reputation: 2557

I guess at some point you'll want to learn a bit more about quoting, specifically single quotes vs double quotes.

Here's the relevant Bash docs: https://www.gnu.org/software/bash/manual/html_node/Quoting.html

 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Awk: Input from one line, execute program; input from next line, execute program C.L. Programming 9 09-27-2010 12:06 AM
[SOLVED] closing parenthesis script cruzdelsur Programming 5 08-30-2010 09:55 AM
delete from open parenthesis to end of line? recomboDNA Programming 1 08-04-2010 04:24 PM
vim matching parenthesis rohr Linux - Software 2 04-23-2005 05:12 PM
Can't rename file with parenthesis in name pleasehelp Linux - Newbie 4 03-16-2004 03:01 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:52 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration