Difference between revisions of "Bash operators"

From Linuxintro
imported>ChrisM
imported>ChrisM
Line 26: Line 26:
 
> and >> can be used to redirect the output (only stdout, not stderr, for that see below) of a command to a file. The difference between them is, that >> appends to the given files, while > will truncate it.
 
> and >> can be used to redirect the output (only stdout, not stderr, for that see below) of a command to a file. The difference between them is, that >> appends to the given files, while > will truncate it.
  
Example:
+
Examples:
 
  ls -alh > files-in-directory.txt
 
  ls -alh > files-in-directory.txt
 
  ls -alh /otherdir >> files-in-directory.txt  # Append list of files in other directory
 
  ls -alh /otherdir >> files-in-directory.txt  # Append list of files in other directory
Line 34: Line 34:
 
| is used to pipe the stdout of one programm to the stdin of another one.
 
| is used to pipe the stdout of one programm to the stdin of another one.
 
  tail -n 100 foo | sort -r  # Reverse sort the last 100 lines of the file file
 
  tail -n 100 foo | sort -r  # Reverse sort the last 100 lines of the file file
 +
 +
= Redirecting stderr =
 +
 +
stderr can be redirected by using 2> or 2>>.
 +
 +
But sometimes, you need stderr to be included in stdout for certain purpose, e.g. when another program is parsing/logging only stdout and you need errors to be in there, too. In these cases, you can redirect stderr to stdout with "2>&1". (Of course, it is also possible to redirect stdout to stderr this way, if you need to silence a program on stdout for some purpose).
 +
 +
Examples:
 +
cat notexisting 2> test  # Writes "File or directory not found." to test
 +
cat notexisting 2>&1  # Outputs "File or directory not found." to stdout

Revision as of 18:03, 31 March 2009

* and ?

You may already know these used for so called "shell globbing" (pattern matching). * replaces an arbitrary amount of characters (including none), while ? replaces exactly one character.

These wildcards are actually bash operators! Imagine, we have the files a1, a2:

cp a*

now is a valid bash command which overrides one of these files with the other one.

$()

The operator $() in the bash shell is replaced by the output of the command enclosed in the parentheses. It is equivalent to backticks (``), but can be cascaded more easily.

Examples:

rpm -ql $(rpm -qa)
for i in $(seq 1 1 100); do echo $i; done

$(())

The operator $(()) in the bash shell is replaced by the arithmetic result of the expression enclosed in the parentheses.

Examples:

# echo $((2*2))
4

> and >>

> and >> can be used to redirect the output (only stdout, not stderr, for that see below) of a command to a file. The difference between them is, that >> appends to the given files, while > will truncate it.

Examples:

ls -alh > files-in-directory.txt
ls -alh /otherdir >> files-in-directory.txt  # Append list of files in other directory

|

| is used to pipe the stdout of one programm to the stdin of another one.

tail -n 100 foo | sort -r  # Reverse sort the last 100 lines of the file file

Redirecting stderr

stderr can be redirected by using 2> or 2>>.

But sometimes, you need stderr to be included in stdout for certain purpose, e.g. when another program is parsing/logging only stdout and you need errors to be in there, too. In these cases, you can redirect stderr to stdout with "2>&1". (Of course, it is also possible to redirect stdout to stderr this way, if you need to silence a program on stdout for some purpose).

Examples:

cat notexisting 2> test  # Writes "File or directory not found." to test
cat notexisting 2>&1  # Outputs "File or directory not found." to stdout