Need help with using strings in variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help with using strings in variable
# 1  
Old 08-24-2015
Need help with using strings in variable

Hi,
I am calling a in-house built api from unix shell script for posting some comments on jira. Facing some problem in handling variables could someone give some insights here.

#1) Below is working

I have declared a variable called var=testing and used that var in my api it works fine.


Code:
$ var=testing
$ echo $var
testing
$ set -x
$ curl -i -H "Content-Type: application/json" -X POST -d '{"jiracomment":{"issuekey":"XXXXX-11454","comment": '\"$var\"'}}' xxx-monhost:5000/comment
+ curl -i -H 'Content-Type: application/json' -X POST -d '{"jiracomment":{"issuekey":"XXXXX-11454","comment": "testing"}}' xxx-monhost:5000/comment

@3) Below one is not working:

I have declared a variable called var="testing testing" and used that var in my api its not working. If you see below its having single quotes between "testing ' 'testing", not sure whar i messed up with quotes.

Code:
 $ var="testing testing"
 $ echo $var
testing testing
$ curl -i -H "Content-Type: application/json" -X POST -d '{"jiracomment":{"issuekey":"XXXXX-11454","comment": '\"$var\"'}}' xxx-monhost:5000/comment
+ curl -i -H 'Content-Type: application/json' -X POST -d '{"jiracomment":{"issuekey":"XXXXX-11454","comment": "testing' 'testing"}}' xxx-monhost:5000/comment
HTTP/1.0 400 BAD REQUEST


When i tried the below it works fine, I think something i mess up with the quotes in #2, any insights will be really helpful.
Code:
$ var="testing testing" 
$ echo "how r u $var"
how r u testing testing


Last edited by rbatte1; 08-25-2015 at 01:10 PM..
# 2  
Old 08-24-2015
Variables don't work in single quotes. Use them in double quotes.

Code:
$ echo '$SHELL'
$SHELL
$ echo "$SHELL"
/bin/bash
$

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to get multiple strings in one variable

I am processing a file using awk to get few input variables which I'll use later in my script. I am learning to script using awk so please advise in any mistakes I made in my code. File sample is as follows # cat junk1.jnk Folder1 : test_file (File) ... (5 Replies)
Discussion started by: shunya
5 Replies

2. Shell Programming and Scripting

How to combine two variable strings?

Hi, I have two variables pscmd="ps -exf | grep " pid=15560 When I say var=$( $pscmd $pid ) echo $var it shows output of all processes on the OS rather than just 15560. So, i guess it only executes "ps -exf | grep " rather than "ps -exf | grep 15560" Can you... (2 Replies)
Discussion started by: mohtashims
2 Replies

3. Programming

Passing full path as argument when it contains variable strings

Hi, In directory "inoutfiles", I have folders fold0001, fold0002 and so on. Every folder has corresponding file file0001.txt, file0002.txt and so on. I want to perform a certain action on multiple files in one go. The cpp file is in the same directory as "inoutfiles". This is my code : ... (1 Reply)
Discussion started by: KidD312
1 Replies

4. Shell Programming and Scripting

Using SED with variable to replace strings in while loop

Hi guys, Hi have this input (Menu.xml)<?xml version="1.0" encoding="ISO-8859-1"?> <breakfast_menu> <food> <name>Berry-Berry Belgian Waffles</name> <price>$8.95</price> <calories>900</calories> </food> <food> <name>French Toast</name> ... (6 Replies)
Discussion started by: cgkmal
6 Replies

5. Shell Programming and Scripting

Replacing variable Text between fixed strings

Hello all, This is my first post and I hope you can help me out. I searched for quite some hours now and haven't found a simple solution to my problem. It is as following: I got this file: dl.dropbox.com/u/14586156/stuff/Bookmarks.plist and want to replace the Text between... (9 Replies)
Discussion started by: pasc
9 Replies

6. Shell Programming and Scripting

Perl removing strings from a variable value

Dear all, I have a variable called $abc, which the value is something like below, *** *********** : ***** where * can be anything. I need to remove all but the final characters until last whitespace. example grd groupstudy : tutor6/7 becomes tutor6/7 something like if... (2 Replies)
Discussion started by: tententen
2 Replies

7. Shell Programming and Scripting

Deciphering strings or variable values

Hi, I have a script at the moment of which reads in simply what the latest version is within a folder i.e. v001, v002, v003 etc and then stores this latest version in a variable i.e. $LATEST would echo v003. I have then cut this string so that I only consider the 003 part. I would then like to... (3 Replies)
Discussion started by: cyberfrog
3 Replies

8. Shell Programming and Scripting

calculate number of strings in a variable

Hi all I have a variable called "variable" and is of the form variable ="AAA BBB CCC DDD" {basically it has values separated by spaces} What is the simplest way to check if "variable" has more that one value in its list? Thanks. (9 Replies)
Discussion started by: felixmat1
9 Replies

9. Shell Programming and Scripting

How to check if two variable are empty strings at once? (bash)

I need to check if $1 or $2 are empty before continuing but I don't know if bash has any logic of the sort. This is what I'm looking for - except that "and" doesn't seem to work. if and ;then ... Thank you! :D (4 Replies)
Discussion started by: ph0enix
4 Replies

10. Shell Programming and Scripting

appending strings to variable

is it possible? as i keep reading a file, i want one particular variable to keep storing the line that i've read so far (1 Reply)
Discussion started by: finalight
1 Replies
Login or Register to Ask a Question