Retain quotes from bash script arguments


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Retain quotes from bash script arguments
# 1  
Old 07-18-2008
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 Smilie

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! Smilie

Danny
# 2  
Old 07-18-2008
Try escaping them:
Code:
myscript.sh grep \"foo bar\"

# 3  
Old 07-18-2008
Thanks for the help, but unfortunately that doesn't work Smilie
Running
> myscript.sh grep \"foo bar\"
gives
> grep: bar": No such file or directory
# 4  
Old 07-18-2008
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 06:50 AM.. Reason: typo
# 5  
Old 07-18-2008
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 (https://www.unix.com/shell-programmin...de-script.html) 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 Smilie

> myscript.sh grep \"foo bar\"
# 6  
Old 07-18-2008
Use single quotes:
Code:
$ cat file
"foo bar"
$ cat myscript.sh
grep "$1" file
$ sh myscript.sh '"foo bar"'
"foo bar"

# 7  
Old 07-18-2008
Hm..
Code:
./mach.ksh yo \"yo\"
  yo "yo"
cat mach.ksh
  echo $@

Works with ksh and bash. What shell do you use?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Passing arguments to interactive program through bash script, here document

Dear Users, I have installed a standalone program to do multiple sequence alignment which takes user parameters to run the program. I have multiple sequence files and want to automate this process through a bash script. I have tried to write a small bash code but its throwing errors. Kindly... (13 Replies)
Discussion started by: biochemist
13 Replies

2. Shell Programming and Scripting

Passing arguments to a bash script

Hi, I wanted to pass an argument to a bash script. So that the argument is used inside the awk command inside the bash script. I know the noraml way of passing argument to a bash script as below : sh myScript.sh abc Inside the bash script i can use like this myArg1=$1 wc $myArg But... (8 Replies)
Discussion started by: shree11
8 Replies

3. Shell Programming and Scripting

Pass arguments to bash script

myscript.sh #!/bin/bash ARGA=$1 if ; then echo "${ARGA}:Confirmed" else echo "${ARGA}:Unconfirmed" fi when I run the above script from the command line, i run it as: ./myscript.sh jsmith now some times, i need to runn it this way: (8 Replies)
Discussion started by: SkySmart
8 Replies

4. Shell Programming and Scripting

Nested double quotes won't work in my bash script?

In a bash script I have: LSCMD="find /project/media/ -mindepth 2 -maxdepth 2 -name \"files*pkg\"" ALL_PACKAGES=$( $LSCMD | sort 2>/dev/null) But I get nothing returned. It's just all blank. If I run the find command in a terminal, I get dozens of hits. I figure it's the way how I'm... (3 Replies)
Discussion started by: superbbrr
3 Replies

5. Shell Programming and Scripting

Bash script with arguments

Could someone help me with the script below? I am trying to make a script having just one arguement as a command and then it executes the appropriate code #!/bin/bash if then echo "Available commands:" echo "./exec.sh cmd1" echo "./exec.sh cmd2" elif then cmd1 =... (1 Reply)
Discussion started by: spiridakos
1 Replies

6. Shell Programming and Scripting

syntax issue with quotes in mysql command for a bash script

i'm trying to write a bash script that executes a mysql statement mysql -sN -e INSERT INTO "$database"."$tableprefix"users (var1, var2,var3) VALUES (123, '1','') i don't know where to put the quotes it doesnt work with this one: ` it seems i can only put double quotes around the... (0 Replies)
Discussion started by: vanessafan99
0 Replies

7. Shell Programming and Scripting

Passing arguments from a bash shell script to a command

I'm pretty new to bash scripting and I've found myself writing things like this (and the same with even more nesting): if $CATEGORIES; then if $LABEL_SLOTS; then $pyth "$wd/texify_grammar.py" "$input" "$texfile" "--label-slots" "--categories" "$CATEGORY_LIST" ... (9 Replies)
Discussion started by: burbly
9 Replies

8. Shell Programming and Scripting

Quotes and arguments

Hello all, I have a very dumb problem while writing a script. I want it to execute the following command, but it's not executed because of wrong quotes. (3 Replies)
Discussion started by: privetq
3 Replies

9. Shell Programming and Scripting

How to make 2 separate arguments in 1 bash script?

This is what I have: #!/bin/bash #ascript.sh WORD1=`tail -n +$1 /home/gscn/word1.txt | head -1` sed -e "s/WORD1/$WORD1/g" < /home/gscn/configtmp > /home/gscn/config WORD2=`tail -n +$1 /home/gscn/word2.txt | head -1` sed -e "s/WORD2/$WORD2/g" < /home/gscn/config2tmp >... (4 Replies)
Discussion started by: guitarscn
4 Replies

10. Shell Programming and Scripting

how to use in bash variables and quotes

I have some troubles with variables and quotes... I want: if $URL is empty (no user input) go to http://www.localhost/index.php/ else add this string (search) "?s=+$URL" EXAMPLE: No user input string= http://www.localhost/index.php/ User input = "unix" string=... (3 Replies)
Discussion started by: aspire
3 Replies
Login or Register to Ask a Question