Interpolate a variable with single quotes


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Interpolate a variable with single quotes
# 1  
Old 05-11-2012
Lightbulb Interpolate a variable with single quotes

I need to interpolate a shell variable in a code, i cannot share the exact code so this is an example i made up to describe the situation

What I am trying to do here is try to wrap up the value of a variable in single quotes. This value needs to be passed to another program which would only process it if it is in single quotes.

This is the code...

Code:
#!/bin/ksh
set -x
 
a=mnop1234
b="'"$a"'"
echo $b
 
prog1 $b

output
Code:
+ a=mnop1234
+ b=''\''mnop1234'\'''
+ echo ''\''mnop1234'\'''
'mnop1234'
+ prog1 ''\''mnop1234'\'''


here, prog1 fails because it is expecting

Code:
prog1 'mnop1234'

instead it gets

Code:
prog1 ''\''mnop1234'\'''

Please help!!
# 2  
Old 05-11-2012
prog1 ''\''mnop1234'\''' is fine.

Please post the contents of prog1. There could be a discrepancy in the way arguments are handled by prog1.
# 3  
Old 05-11-2012
Sorry cannot share the contents of prog1 because
1. it is binary
2. it is copyrighted

Sorry but I can share only limited info on this due to security issues.

Also,

Code:
 
prog1 ''\''mnop1234'\'''

doesn't work
it will only work in this format

Code:
 
prog1 'mnop1234'

# 4  
Old 05-11-2012
Ok, here's an example to clear things up. Let us know if this is what you're looking for:

Code:
[root@host dir]# cat test.sh
#! /bin/bash
echo $1
[root@host dir]# ./test.sh 'mnop1234'
mnop1234
[root@host dir]# ./test.sh ''\''mnop1234'\'''
'mnop1234'
[root@host dir]#

# 5  
Old 05-11-2012
It is the same:

Code:
$ a=''\''mnop1234'\'''
$ echo "$a"
'mnop1234'

Code:
$ b=mnop1234
$ a="'$b'"
$ echo "$a"
'mnop1234'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Expansion of variable inside Single Quotes Fails!!

I am unable to expand the value of entry variable inside the nawk command. I tried three different nawk command as below but none of them substitute the value of entry variable. ls *.txt | while IFS='' read -r entry; do #nawk '/<name>/{A=1;++i} A{print >> ("cmd"i"_"$entry)}... (9 Replies)
Discussion started by: mohtashims
9 Replies

2. Shell Programming and Scripting

Issue with Single Quotes and Double Quotes for prompt PS1

Hi, Trying to change the prompt. I have the following code. export PS1=' <${USER}@`hostname -s`>$ ' The hostname is not displayed <abc@`hostname -s`>$ uname -a AIX xyz 1 6 00F736154C00 <adcwl4h@`hostname -s`>$ If I use double quotes, then the hostname is printed properly but... (3 Replies)
Discussion started by: bobbygsk
3 Replies

3. UNIX for Dummies Questions & Answers

awk for inserting a variable containing single and double quotes

Hi i have to insert the below line into a specific line number of another file export MBR_CNT_PRCP_TYPE_CODES_DEL="'01','02','04','05','49','55','UNK'" I have passed the above line to a variable say ins_line. I have used below command to perform the insert awk 'NR==3{print "'"${ins_line}"'"}1'... (1 Reply)
Discussion started by: sathishteradata
1 Replies

4. Shell Programming and Scripting

Having a terrible problem with quotes/single quotes!

Hello. I'm trying to write a bash script that uses GNU screen and have hit a brick wall that has cost me many hours... (I'm sure it has something to do with quoting/globbing, which is why I post it here) I can make a script that does the following just fine: test.sh: #!/bin/bash # make... (2 Replies)
Discussion started by: jondecker76
2 Replies

5. Shell Programming and Scripting

Replace single quote with two single quotes in perl

Hi I want to replace single quote with two single quotes in a perl string. If the string is <It's Simpson's book> It should become <It''s Simpson''s book> (3 Replies)
Discussion started by: DushyantG
3 Replies

6. UNIX for Dummies Questions & Answers

grep single quotes or double quotes

Unix superusers, I am new to unix but would like to learn more about grep. I am very familiar with regular expressions as i have used them for searching text files in windows based text editors. Since I am not very familiar with Unix, I dont understand when one should use GREP with the... (2 Replies)
Discussion started by: george_vandelet
2 Replies

7. Shell Programming and Scripting

while puting shell variable in mysql command value does not interpolate

port=$(ssh tms6@$x cat /tms6/scripts/start.lc.sh | grep -P '^\/tms6\/bin\/lc' | cut -d' ' -f3 | cut -b 3-6) tpsip=$(ssh tms6@$x cat /tms6/scripts/start.lc.sh | grep -P '^\/tms6\/bin\/lc' | cut -d' ' -f4 | cut -b 9-) # IFS="\n" set -- $port ... (1 Reply)
Discussion started by: rrd1986
1 Replies

8. Shell Programming and Scripting

Single quotes and double quotes

Hi guys, I have a sed line in double quotes which works fine, but I want it to be in single quotes here is the sed line sed "/abc_def/s/\'.*\'/\'\${abc_def}\'/" can some one give the equivalent to the above script in single quotes Thanks a ton (5 Replies)
Discussion started by: sol_nov
5 Replies

9. Shell Programming and Scripting

Double quotes or single quotes when using ssh?

I'm not very familiar with the ssh command. When I tried to set a variable and then echo its value on a remote machine via ssh, I found a problem. For example, $ ITSME=itsme $ ssh xxx.xxxx.xxx.xxx "ITSME=itsyou; echo $ITSME" itsme $ ssh xxx.xxxx.xxx.xxx 'ITSME=itsyou; echo $ITSME' itsyou $... (3 Replies)
Discussion started by: password636
3 Replies

10. Shell Programming and Scripting

echo using single quotes

Hi, Please help me to echo the following statement using single quotes Why can't I write 's between single quotes Thanks in advance, Chella (3 Replies)
Discussion started by: chella
3 Replies
Login or Register to Ask a Question