Tutorial by Examples

> redirect the standard output (aka STDOUT) of the current command into a file or another descriptor. These examples write the output of the ls command into the file file.txt ls >file.txt > file.txt ls The target file is created if it doesn't exists, otherwise this file is truncated. ...
< reads from its right argument and writes to its left argument. To write a file into STDIN we should read /tmp/a_file and write into STDIN i.e 0</tmp/a_file Note: Internal file descriptor defaults to 0 (STDIN) for < $ echo "b" > /tmp/list.txt $ echo "a" >> ...
File descriptors like 0 and 1 are pointers. We change what file descriptors point to with redirection. >/dev/null means 1 points to /dev/null. First we point 1 (STDOUT) to /dev/null then point 2 (STDERR) to whatever 1 points to. # STDERR is redirect to STDOUT: redirected to /dev/null, # effect...
2 is STDERR. $ echo_to_stderr 2>/dev/null # echos nothing Definitions: echo_to_stderr is a command that writes "stderr" to STDERR echo_to_stderr () { echo stderr >&2 } $ echo_to_stderr stderr
Truncate > Create specified file if it does not exist. Truncate (remove file's content) Write to file $ echo "first line" > /tmp/lines $ echo "second line" > /tmp/lines $ cat /tmp/lines second line Append >> Create specified file if it does not e...
Commands have one input (STDIN) and two kinds of outputs, standard output (STDOUT) and standard error (STDERR). For example: STDIN root@server~# read Type some text here Standard input is used to provide input to a program. (Here we're using the read builtin to read a line from STDIN.) STDO...
{ echo "contents of home directory" ls ~ } > output.txt
Sometimes you may want to output something by one program and input it into another program, but can't use a standard pipe. ls -l | grep ".log" You could simply write to a temporary file: touch tempFile.txt ls -l > tempFile.txt grep ".log" < tempFile.txt This work...
Error messages are generally included in a script for debugging purposes or for providing rich user experience. Simply writing error message like this: cmd || echo 'cmd failed' may work for simple cases but it's not the usual way. In this example, the error message will pollute the actual output...
2.04 Bash treats some paths as special and can do some network communication by writing to /dev/{udp|tcp}/host/port. Bash cannot setup a listening server, but can initiate a connection, and for TCP can read the results at least. For example, to send a simple web request one could do: exec 3</...

Page 1 of 1