1

I am working with scripts that output xml and I am using xmllint to format the output. Xmllint requires a source argument, so I use the hyphen to pipe standard output to it:

$> script.php source.txt | xmllint --format - > nice_output.xml

Since I run these scripts quite a bit, I would like to turn the xmllint --format - part into a single command that I can pipe into, but I am not sure how to even start. I would like to do this:

$> script.php source.txt | nicexml > nice_output.xml

Where nicexml is my custom command/alias. How do I do this?

  • 1
    Doesn't a plain old alias work? (- has no shell magic, it's passed as-is to the process as an argument.) – Mat Mar 13 '13 at 20:32
  • I tried an alias from H.-Dirk Schmitt's answer but it didn't work; I didn't see what I did wrong. – user394 Mar 13 '13 at 21:13
  • 1
    Never say just "didn't work". That can't help anyone help you fix anything. Describe the error/misbehavior. – Mat Mar 13 '13 at 21:15
  • I got a blank output with no error, with and without preceding dollar sign. – user394 Mar 13 '13 at 21:18
  • I guess I didn't restart properly enough, it works now (got expected output) :) – user394 Mar 13 '13 at 21:22
3

alias version

alias nicexml="xmllint --format -"

bash function

function nicexml() { xmllint --format "${@:--}"; }
  • Default input is stdin
  • Allows also alternative input source and options.
  • How do I call it as an alias? I put that line in my .bash_rc, started a new bash session, but it doesn't seem to work, either as nicexml or $nicexml. – user394 Mar 13 '13 at 20:41
  • The alias as : script.php source.txt | nicexml , but take care - in the 1st version was a small typo. – H.-Dirk Schmitt Mar 13 '13 at 20:47
  • 1
    The function is better - it supports script.php source.txt | nicexml and nicexml myFile.xml – H.-Dirk Schmitt Mar 13 '13 at 20:48
0

You could define a shell variable nicexml="xmllint --format -" and then use it as $nicexml.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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