Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
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

2 Answers 2

up vote 3 down vote accepted

alias version

alias nicexml="xmllint --format -"

bash function

function nicexml() { xmllint --format "${@:--}"; }
  • Default input is stdin
  • Allows also alternative input source and options.
share|improve this answer
    
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

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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