The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Double quotes or single quotes when using ssh? password636 Shell Programming and Scripting 3 05-29-2008 08:52 PM
escaping double-quotes inside the script? GKnight Shell Programming and Scripting 3 05-05-2008 05:35 PM
Bash Shell - # of arguments DNAx86 Shell Programming and Scripting 1 01-10-2008 01:23 PM
Bash: Reading 2 arguments from a command line Vozx Shell Programming and Scripting 0 12-08-2005 05:23 PM
retain Line numbers.. in Vi .. OR .. A SHELL SCRIPT sdlayeeq Shell Programming and Scripting 2 04-26-2005 04:20 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-18-2008
cypression cypression is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 4
Retain quotes from bash script arguments

Hi there,

I've been scouring these forums and have found similar threads, but none apparently helped me solved my problem

I'd like to run a command within a bash script, but that command is provided by the user to the script and may contain quotes, which is where the problem lies.

My script is (fundamentally):

-----
#!/usr/bin/bash

commandToRun="$@"

$commandToRun ~/t.txt
-----

And I'd like to run it using the following:

> myscript.sh grep "foo bar"

But the quotes are removed by the time I try to run that command from within the script, i.e. the $commandToRun $filename line and so it tries to run
> grep foo bar
instead of
> grep "foo bar" ~/t.txt

Thanks for any help, it would be much appreciated!

Danny
  #2 (permalink)  
Old 07-18-2008
zaxxon's Avatar
zaxxon zaxxon is offline Forum Staff  
Moderator
  
 

Join Date: Sep 2007
Location: Germany
Posts: 2,259
Try escaping them:
Code:
myscript.sh grep \"foo bar\"
  #3 (permalink)  
Old 07-18-2008
cypression cypression is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 4
Thanks for the help, but unfortunately that doesn't work
Running
> myscript.sh grep \"foo bar\"
gives
> grep: bar": No such file or directory
  #4 (permalink)  
Old 07-18-2008
cypression cypression is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 4
Hmm, I also tried changing the line that runs the command in the script to:
"$commandToRun" ~/t.txt
(which is arguably something I should've done before anyway), but now I get the following:

> myscript.sh grep "foo bar"
gives
grep foo bar: command not found

> myscript.sh grep \"foo bar\"
gives
grep "foo bar": command not found

Thanks again though :S

Last edited by cypression; 07-18-2008 at 05:50 AM.. Reason: typo
  #5 (permalink)  
Old 07-18-2008
cypression cypression is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 4
Aha!
I don't why I didn't spot this thread before when I was searching, but one of the threads in the 'More UNIX and Linux Forum Topics You Might Find Helpful' section (escaping double-quotes inside the script?) helped me solve it using a simple eval

So in the script, I now have:

----
#!/usr/bin/bash

commandToRun="$@"

eval "$commandToRun" ~/t.txt
----

I still have to run the command with the quotes escaped as zaxxon suggested, but at least it works now

> myscript.sh grep \"foo bar\"
  #6 (permalink)  
Old 07-18-2008
danmero danmero is offline Forum Advisor  
  
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 1,420
Use single quotes:
Code:
$ cat file
"foo bar"
$ cat myscript.sh
grep "$1" file
$ sh myscript.sh '"foo bar"'
"foo bar"
  #7 (permalink)  
Old 07-18-2008
zaxxon's Avatar
zaxxon zaxxon is offline Forum Staff  
Moderator
  
 

Join Date: Sep 2007
Location: Germany
Posts: 2,259
Hm..
Code:
./mach.ksh yo \"yo\"
  yo "yo"
cat mach.ksh
  echo $@
Works with ksh and bash. What shell do you use?
Closed Thread

Bookmarks

Tags
bash, bash eval, eval, linux

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 01:14 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0