pavement

Bash

From FreeBSDwiki
(Difference between revisions)
Jump to: navigation, search
(Redirection in bash)
Line 7: Line 7:
 
  samizdata# myprogram 2> file  
 
  samizdata# myprogram 2> file  
 
will send only the '''error''' output of ''myprogram'' to ''file''
 
will send only the '''error''' output of ''myprogram'' to ''file''
  samizdata# myprogram 2>&1 | command
+
  samizdata# myprogram 2>&1 | command2
 
will send errors (descriptor 2,) to the same place as output (descriptor 1) and then pipe that to ''command2''
 
will send errors (descriptor 2,) to the same place as output (descriptor 1) and then pipe that to ''command2''
  

Revision as of 01:50, 30 August 2004

The Bourne Again Shell (located in /bin/bash) is the default shell of the Linux operating system and is the shell that users of that system will likely be most familiar with.

Redirection in bash

Bash's shining moment comes for users who need to see and/or redirect errors to somewhere other than STOUT (your console). Standard input (STIN), standard output (STOUT) and standard error (STERR) are by default sent to the same place: STOUT (on most systems, this will be your console or tty). bash labels these descriptors 0, 1 and 2, respectively -- you have 10 descriptors, but only 0-2 are actually taken by anything. So

samizdata# myprogram > file

will send the output of myprogram to file

samizdata# myprogram 2> file 

will send only the error output of myprogram to file

samizdata# myprogram 2>&1 | command2

will send errors (descriptor 2,) to the same place as output (descriptor 1) and then pipe that to command2

Okay, let's say you want to send output to your screen and errors to a file. You can't just do

samizdata# myprogram 1>&2 2>&1 > errors.txt

because when you do the first switch, it's done right away and when the second >& comes around, it's getting the switched data. This is where the other, normally unused, file descriptors 3-9 come in. You can use them as place-holders, such as:

samizdata# myprogram 3>&2 2>&1 1>&3 | command

will make the output of myprogram do this: 3 point to the same place as 2, 2 point to 1, and finally, 1 point to 3 and then pipe all of it to command

Seen bash's man page for more info.


Note: Bash is not available by default in the base system, but can easily be installed from ports if desired.

Other shells that you can install and customize for ease of use are the bash, tcsh, psh, ksh, zsh.

see also: bash homepage

Personal tools