need to enclose a string in quotes


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting need to enclose a string in quotes
# 1  
Old 08-09-2011
need to enclose a string in quotes

I have a script which I call and pass a text string to it. This string is then is assigned to a variable in the script. I then call another script and pass that variable to the second script, but when I do, the quotes are lost and the second script gets a total of three variables 'my', 'lovely' and 'string' instead of one whole "my lovely string".
Quote:
./script1.sh "my lovely string"
script1.sh
Code:
#!/bin/ksh
var1=$1
echo var1 is $var1
./script2.sh \"$var1\"

script2.sh
Code:
#!/bin/ksh

if [ ! "$1" = "" ]
then
        echo var1 is $1
fi

The whole sequence when run in debug mode looks like this:
Quote:
sh -x script1.sh "testing my changes"
+ var1='testing my changes'
+ echo var1 is testing my changes
var1 is testing my changes
+ ./script2.sh '"testing' my 'changes"'
var1 is "testing
Note the extra single quotes in when script2.sh is called. So my question is is there a way to preserve my original string as a whole while passing it on to script2.sh? An alternative would be to replace spaces with underscores, but that would be lame and I'd like to be able to support spaces in the string. Any pointers are appreciated.
# 2  
Old 08-09-2011
Why do you escape double quotes in ./script2.sh \"$var1\" ? Without backslashes everything should work. But the idiomatic way to pass shell script arguments to another command is:
Code:
./script2 "$@"

And extra single quotes are the result of working 'set -x'.
# 3  
Old 08-09-2011
Those aren't extra single quotes. That's just the shell tracing making explicit what's going on.

If you want to pass the value of $var1 with literal quotes to the second script, you need to do the following:
Code:
./script2.sh \""$var1"\"

Aside from the backslash-escaped set of quotes that you intend to pass literally, you must use an unescaped pair to protect $var1 from subsequent field splitting and file globbing.

Regards,
Alister
# 4  
Old 08-09-2011
Quote:
Originally Posted by alister
Those aren't extra single quotes. That's just the shell tracing making explicit what's going on.

If you want to pass the value of $var1 with literal quotes to the second script, you need to do the following:
Code:
./script2.sh \""$var1"\"

Aside from the backslash-escaped set of quotes that you intend to pass literally, you must use an unescaped pair to protect $var1 from subsequent field splitting and file globbing.

Regards,
Alister
Thanks, this combination of quotes/escapes worked!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Enclose String in single quote

I have a comma separated file which contains data like; File header: ID_WVR,SAK_WVR_SVC,DSC_WVR_WVC,SAK_PROCEDURE,CODES,CDE_PROC_MOD ,CDE_PROC_MOD_2 ,CDE_PROC_MOD_3 File Detail: AMR,5100,Total Services,305,D0120,,, AMR,5101,Periodic Services,40702,H2011,U1,, AMR,5112,Day... (4 Replies)
Discussion started by: jnrohit2k
4 Replies

2. Shell Programming and Scripting

How to capture a string enclose by a pattern within a file?

Hi all, My file :test.txt just like this: ........................... From: 333:123<sip:88888888888@bbbb.com To: <sip:123456@aaaaa.com ......................... I want a script to capture the string between sip: & @ Expect output: 88888888888 123456 Please help! (4 Replies)
Discussion started by: Alex Li
4 Replies

3. Shell Programming and Scripting

awk : match the string and string with the quotes :

Hi all, Here is the data file: - want to match only lan3 in the output . - not lan3:1 file : OPERATING_SYSTEM=HP-UX LOOPBACK_ADDRESS=127.0.0.1 INTERFACE_NAME="lan3" IP_ADDRESS="10.53.52.241" SUBNET_MASK="255.255.255.192" BROADCAST_ADDRESS="" INTERFACE_STATE=""... (2 Replies)
Discussion started by: rveri
2 Replies

4. Shell Programming and Scripting

How to find a string with double quotes?

I have thousands of files in a directory. I need to find/list all files that have the below matching string - RETURNCODE: "1017" Thank you! (5 Replies)
Discussion started by: esmgr
5 Replies

5. Shell Programming and Scripting

String between quotes

Hi, Need to capture a string between 1st & last quote. String can be anything like a b "c" d e Output: c a "b c" d e Output: b c a "b c" d "e" f Output: b c d e sed 's/.*"\(.*\)".*/\1/g' Above helps me to find the string between last quote. Need to find the string between 1st &... (7 Replies)
Discussion started by: vibhor_agarwali
7 Replies

6. Shell Programming and Scripting

Get string between quotes separate by commas

I'm a beginner with shell and tried to do this per hours and everytinhg gives different want i do. So I have a lot of file in *.csv ( a.csv, b.csv ...) in each file csv , it has some fields separeted by commas. ----- "joseph";"21","m";"groups";"j.j@gmail.com,j.j2@hotmail.com"... (6 Replies)
Discussion started by: flaviof
6 Replies

7. Shell Programming and Scripting

Enclose words between double quotes

My input is like this: this is a test line. I want my output to be like this: "this", "is", "a", "test", "line" Any idea how this can be done in Linux? (3 Replies)
Discussion started by: shoaibjameel123
3 Replies

8. Shell Programming and Scripting

Add double quotes around the string

I have a line in multiple scripts:select into table /dir1/dir2/file.dat dir1 and dir2 are the same but file.dat is different from script to script. I need to include /dir1/dir2/file.dat into double quotes in each file of my directory:select into table "/dir1/dir2/file.dat" (13 Replies)
Discussion started by: surfer515
13 Replies

9. UNIX for Dummies Questions & Answers

String concat that keeps quotes

Hi All, I hope you can help. i am concatenating String variables using the following method. command="$command$x" i have created a script which takes a set of args passed to the script using the $* #example script args=$* count=0 for x in $args do count=`expr $count + 1` ... (8 Replies)
Discussion started by: duke
8 Replies

10. UNIX for Dummies Questions & Answers

Add single quotes in string

Hi All, I love this site, it helps newbie people like me and I appreciate everyone's help! Here is my questions. I am trying to concatenate a single quote into a character/string from a text file for each line (lets say ABC should look like 'ABC'). I tried to use awk print command to do... (1 Reply)
Discussion started by: mrjunsy
1 Replies
Login or Register to Ask a Question