0

This question already has an answer here:

I have a shell script that executes a jar file and stores the output generated by executing the jar file to a variable.

E.g:

myvariable=$(java -jar MyJavaJarFile.jar)

However the output when I execute the java statement given below consists of special characters such as /, \, +, =, etc. But when it is stored to the variable characters such as \ is omitted from the variable.

java -jar MyJavaJarFile.jar

If the actual output is abc=\=abc, then the variable stores only abc==abc.

Could someone help me out with this? Thanks.. :)

marked as duplicate by Gilles bash Sep 28 '16 at 22:14

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Works for me: x=$(echo 'abc=\=abc'); echo $x – choroba Sep 27 '16 at 8:34
1

Command substitution ($(cmd) or `cmd` (also ${ cmd;} in ksh93)) stores the standard output of cmd minus the trailing newline characters (unless that output contains NUL bytes in shells other than zsh).

It does nothing special with backslash. The only characters it may mess-up with are the NUL character (and every character past the first NUL occurrence in some shells) and those trailing newline characters.

var=$(printf '%s\n\n\n' 'foo\bar\\baz')

Will store foo\bar\\baz in $var.

If you don't see those backslashes in $var that would be either because you didn't check if they're there properly like by using an echo or print implementation that interprets backslash escape sequences.

For instance in UNIX conformance mode, bash's echo gives:

bash-4.3$ echo "$var"
foar\baz

(the \b was expanded to a backspace character which when sent to a terminal makes it go backward one character).

You want to use printf to output arbitrary data (and don't forget to quote your variables):

bash-4.3$ printf '%s\n' "$var"
foo\bar\\baz

And when that output goes to a terminal, you may want to post-process it with sed -n l so control characters or trailing space characters for instance become visible.


Or it's because those backslashes were not output on stdout. For instance:

foo() {
  printf foo
  printf '\\' >&2
  printf 'bar\n'
}

When running that function:

bash-4.3$ foo
foo\bar
bash-4.3$ var=$(foo)
\bash-4.3$ printf '%s\n' "$var"
foobar

The \ did not make it to $var it was output to stderr.


Or of course it could be because they're not output at all as your comment to Benjo's answer suggests, but then that would be a problem in your java application, not bash.

0

Try

myvariable="$(java -jar MyJavaJarFile.jar)"

And it should store it as a string which should show the "\" aswell

  • Thanks.. But it isn't working for me.I just tried to execute java -jar MyJavaJarFile.jar directly on the bash prompt and its output doesn't show the "\" character as well.. – Abraham Jaison Sep 27 '16 at 8:40
  • No, the "s make no difference in this case as it's an assignment to a scalar variable where there can't be any split+glob involved. – Stéphane Chazelas Sep 27 '16 at 8:47
  • @StéphaneChazelas Ahh, okay, now i understand, thanks – Benjo Sep 27 '16 at 8:54

Not the answer you're looking for? Browse other questions tagged or ask your own question.