LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 04-02-2008, 04:57 PM   #1
jakeo25
LQ Newbie
 
Registered: Jan 2008
Location: Boulder, CO
Distribution: Ubuntu Gusty / Fiesty
Posts: 7

Rep: Reputation: 0
Parenthesis and other odd characters passed to bash function


I would like to code a function which will accept a '(' in the parameter, and do some stream editing in the process. So for example:
------------------------------------------------------------------------
#!/bin/bash

function foo ()
{
# use the entire commandline ($@) as a single argument
## this allows spaces
output=`echo "$@" | sed 's|(|\\\(|g'`
echo $output
}

foo Hello (world)
-----------------------------------------------------------------------

If I do this, bash will complain about the '(' and ')'

output:

./foobar.sh: line 11: syntax error near unexpected token `('
./foobar.sh: line 11: `foo Hello (world)'

There are other characters, such as '*', which cause problems.

The way around this is to use double quotes in the call:

foo "Hello (world)"

output:

Hello \(world)

So my question is: How can I re-write foo() to avoid using the double quotes when I make the call?


Thanks in advance

Jake
 
Old 04-02-2008, 05:08 PM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,503

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Quote:
Originally Posted by jakeo25 View Post
So my question is: How can I re-write foo() to avoid using the double quotes when I make the call?
You can't. The error is generated before the function can do anything (indeed the function is not called). Since parentheses have a special meaning in bash, you must escape them for literal interpretation. You can use double quotes, single quotes or the escape character. Anyway, what's wrong in using double quotes?
 
Old 04-03-2008, 10:27 AM   #3
jakeo25
LQ Newbie
 
Registered: Jan 2008
Location: Boulder, CO
Distribution: Ubuntu Gusty / Fiesty
Posts: 7

Original Poster
Rep: Reputation: 0
Thanks for the reply.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
spaces and odd characters in directory name in bash? babag Programming 1 04-02-2008 03:28 PM
Using C API; first query passed passed by program to MySQL is successful, second quer weibullguy Programming 2 06-12-2007 10:45 PM
Bash looping or function within a function FirmbIT Programming 2 04-24-2006 01:06 PM
Array structs - passed to function cdog Programming 4 02-02-2006 03:07 PM
bash script ? -- spaces in passed parameters azwr Linux - Newbie 3 06-18-2004 06:57 PM


All times are GMT -5. The time now is 12:04 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration