-1

I've a variable, wich contains all types of characters

myvar="alfanum;001String"

and I need to pass them to a command with a single quotes in this way

java -jar myapp.jar  HERE I NEED TO PUT ->  'alfanum;001String' with single quotes

but if I do

java -jar myapp.jar "'$myvar'"  

it doesn't work.

Note I can't use just double quotes:

java -jar myapp.jar "$myvar" 

because $myvar is a user's input and myapp.jar need to retrieve it rounded with a single quotes

Thank you

fedorqui
  • 275,237
  • 103
  • 548
  • 598
user2548436
  • 915
  • 2
  • 16
  • 35
  • 1
    Quite related: [Expansion of variable inside single quotes in a command in bash shell script](http://stackoverflow.com/q/13799789/1983854). So something like `java -jar myapp.jar ''"$var"''` should do – fedorqui Aug 12 '16 at 08:47
  • Your first code with `"'$myvar'"` should work. – Barmar Aug 12 '16 at 09:06
  • Unfortunatelly no, otherwise I would not opened this question :) – user2548436 Aug 12 '16 at 09:08
  • 1
    What doesn't work and what is error? – anubhava Aug 12 '16 at 09:49
  • If the *argument* needs to contain single quotes, it's the caller's responsibility to provide the correct data, not yours. If you weren't using `myvar`, would the correct call be `java -jar myapp.jar 'alfanum;001String'` or `java -jar myapp.jar "'alfanum;001String'"`? – chepner Aug 12 '16 at 11:45

2 Answers2

2

You can mix several quote types together to form a string in singe quotes:

"'""$myvar""'" will expand to 'alfanum;001String'. You're probably need just "$myvar" here though.

featuredpeow
  • 2,061
  • 1
  • 19
  • 18
0

You can do it like this

java --jar myapp.jar $(echo "'$myvar'")

But why not just use double quotes?

sge
  • 7,330
  • 1
  • 15
  • 18
  • 1
    The command substitution needs to be quoted, but in `bash`, this is exactly the same as `java -jar myapp.jar "'$myvar'"`. – chepner Aug 12 '16 at 11:49