pavement

Bash

From FreeBSDwiki
(Difference between revisions)
Jump to: navigation, search
Line 1: Line 1:
 
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.
 
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.
 +
 +
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# command > file
 +
will send the output of ''command'' to ''file''
 +
samizdata# command 2> file
 +
will send only the '''error''' output of ''command'' to ''file''
 +
samizdata# command 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# command 3>&2 2>&1 1>&3 | command2
  
 
Note: Bash is ''not'' available by default in the base system, but can easily be installed from [[:Category:Ports and Packages|ports]] if desired.
 
Note: Bash is ''not'' available by default in the base system, but can easily be installed from [[:Category:Ports and Packages|ports]] if desired.

Revision as of 23:28, 29 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.

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# command > file

will send the output of command to file

samizdata# command 2> file 

will send only the error output of command to file

samizdata# command 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# command 3>&2 2>&1 1>&3 | command2

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