Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unix pipes and positional arguments

Tags:

bash

unix

pipe

I'm combining sox and lame to generate a new music file, but in order to do everything on one line using pipes, it seems necessary to 'mark' the output and input boundaries with a - character. I've inherited this code, so let me show.

sox $DIRNAME/$BASENAME -e signed-integer -r 8000 -c 2 -t wav - trim $POSITIONS | lame -v -V4 --resample 8 - $DIRNAME/${NOEXT}.mp3

The - between wav and trim is the output file, and the - between --resample 8 and $DIRNAME/${NOEXT}.mp3 is the input file.

I'm trying to find further information on this, like whether any character can be used, or if - is special in this way. What is this called, and what makes it work?

like image 562
Josh Smeaton Avatar asked Oct 01 '12 01:10

Josh Smeaton


3 Answers

Many Unix command-line utilities use "-" as a shorthand to mean "don't use a real file here, use stdin (or stdout) instead". Sox is one of these programs:

This is from the sox manpage

SoX can be used in simple pipeline operations by using the special filename '-' which, if used in place of an input filename, will cause SoX will read audio data from 'standard input' (stdin), and which, if used in place of the output filename, will cause SoX will send audio data to 'standard output' (stdout). Note that when using this option, the file-type (see -t below) must also be given.

like image 92
Tom W Avatar answered Oct 29 '22 10:10

Tom W


By convention, unix and friends use - to represent stdin and stdout.

It's not 100% universal, but it's a pretty widely used.

like image 37
John3136 Avatar answered Oct 29 '22 09:10

John3136


In your example, it's the same thing as

/dev/stdin

Try to replace your - with it, you will see.

like image 28
Gilles Quenot Avatar answered Oct 29 '22 09:10

Gilles Quenot