1

According to the Xidel documentation, I'm under the impression that the following code should work, and should produce an output that I can access in the BASH variable "bar":

#!/bin/bash


TEST='<foo><bar>test</bar></foo>'
xidel $TEST -e 'bar:=//bar' --output-format=bash
echo "Result: ${bar}"

However, the output I get when executing this code is:

**** Processing: data:,<foo><bar>test</bar></foo> ****
** Current variable state: **
bar='test'
Result:

It's strange because Xidel is clearly printing its current variable state to include my variable...so I must not be accessing it correctly? How do I access it now, if not with $bar

Edit:

Gordon Davisson's comment below is correct - I misread the documentation. Working code looks like this:

#!/bin/bash


TEST='<foo><bar>test</bar></foo>'
eval $(xidel $TEST -e 'bar:=//bar' --output-format=bash)
echo "Result: ${bar}"
Cody S
  • 4,744
  • 8
  • 33
  • 64
  • 1
    No external program like xidel is able to change variables in the environment from which it was called. – Cyrus Apr 19 '22 at 19:58
  • 1
    Xidel is *printing* its variable state to the terminal, which has no effect on the shell running the Xidel command. I'm not familiar with Xidel and its output formats, but you probably have to do something like `eval "$(xidel "$TEST" -e 'bar:=//bar' --output-format=bash)" to execute the output (instead of printing it). – Gordon Davisson Apr 19 '22 at 20:41
  • @GordonDavisson Yep, that was my mistake. Thank you. If you put that into an answer, I'll mark it as accepted – Cody S Apr 19 '22 at 21:07
  • 1
    @CodyS If Philippe's answer works, I'd use that instead. Anything involving `eval` tends to have edge cases where it fails in bizarre and even dangerous ways; Philippe's answer is much safer. – Gordon Davisson Apr 20 '22 at 02:25
  • 1
    Numerous correct comments above, but a good habit to get into is to dbl-quote all variable usage, ie `"$TEST"`, as done several places here in the responses.. Also is worthwhile to always check and correct your code for other basic syntax problems by copy/pasting into [shellcheck](https://shellcheck.net). (Be sure to include the correct "she-bang" line as first line, i.e. `#!/bin/bash`.) Good luck. – shellter Apr 20 '22 at 04:08

1 Answers1

1

Is this what you want to achieve ?

TEST='<foo><bar>test</bar></foo>'
bar="$(xidel "$TEST" -e '//bar')"
echo "Result: ${bar}"
Philippe
  • 20,025
  • 2
  • 23
  • 32