Linux shell programming : input/output redirection
Linux system commands take input from our terminal and send the output back to our terminal. A command normally reads its input from standard input (terminal) by default. Similarly, a command writes its output to standard output (terminal) by default.
The output from a command can be easily redirected to a file instead of a terminal:
$ ls > files
We didn't get any output on our terminal because the output has been redirected from the default standard output device (the terminal) into the specified file:
$ ls > files $ cat files arr2.sh arr.sh ... unknown_args.sh v.sh
Since '>' overwrites, we may want to use '>>' to append to an existing file.
Just as the output of a command can be redirected to a file, so can the input of a command be redirected from a file. As the greater-than character '>' is used for output redirection, the less-than character '>' is used to redirect the input of a command.
The commands that normally take their input from standard input can have their input redirected from a file in this manner. For example, to count the number of lines in the file users generated above, we can execute the command as follows:
$ wc -l file_list 16 files
We can count the number of lines in the 'files' by redirecting the standard input of the wc -l
command:
$ wc -l < file_list 16
Note that there is a difference in the output produced by the two forms of the wc -l
command. In the first case, the number of the files in the "file_list" is displayed with the line count; in the second case, it is not.
In the first case, wc
knows that it is reading its input from the 'file_list'. In the second case, it only knows that it is reading its input from standard input so it does not display 'files'.
A here document
is a special-purpose code block. It uses a form of I/O redirection to feed a command list to an interactive program or a command, such as ftp, cat, or the ex text editor.
COMMAND <<InputComesFromHERE ... ... ... InputComesFromHERE
Following is the input to the command wc -l
to count total number of line:
$ wc -l << EOF > Whose woods these are I think I know. > His house is in the village, though; > He will not see me stopping here > To watch his woods fill up with snow. > My little horse must think it queer > To stop without a farmhouse near > Between the woods and frozen lake > The darkest evening of the year. > EOF 8
Another example that writes a shell script file using "cat" and the "here document":
$ cat <<EOF > myscript.sh #!/bin/bash echo $PWD EOF
This creates "myscript.sh" and it looks like this:
#!/bin/bash echo /Users/kihyuckhong/Documents/TEMP
Or easier form by switching EOF with file name:
$ cat > myscript.sh <<EOF #!/bin/bash echo $PWD EOF
Some people like to use '-' to indicate stdin/stdout (Usage of dash (-) in place of a filename):
$ cat - > myscript.sh <<EOF #!/bin/bash echo $PWD EOF
When we execute a program, Linux opens three file descriptors for the program:
- 0 standard input (stdin)
- 1 standard output (stdout)
- 2 standard error (stderr)
The redirect output symbol (>) is shorthand for 1>, which tells the shell to redirect standard output. Similarly, < is short for 0<, which redirects standard input. The symbol 2> redirects standard error.
There are cases when we need to execute a command, but we don't want the output displayed to the screen. In such cases, we can discard the output by redirecting it to the file /dev/null which is a special file that automatically discards all its output:
$ command > /dev/null
Actually, stdout
goes to /dev/null (which is a dummy device that does not record any output).
To discard both output of a command and its error, we want to redirect stderr
to stdout
:
$ command > /dev/null 2>&1
Here 2 represents stderr
and 1 represents stdout
. By specifying 2>&1 the stderr
also goes to the stdout
(which is already redirected to /dev/null).
We can display a message on to stderr
by redirecting stdin
into stderr
as follows:
$ echo message 1>&2
Linux - system, cmds & shell
- Linux Tips - links, vmstats, rsync
- Linux Tips 2 - ctrl a, curl r, tail -f, umask
- Linux - bash I
- Linux - bash II
- Linux - Uncompressing 7z file
- Linux - sed I (substitution: sed 's///', sed -i)
- Linux - sed II (file spacing, numbering, text conversion and substitution)
- Linux - sed III (selective printing of certain lines, selective definition of certain lines)
- Linux - 7 File types : Regular, Directory, Block file, Character device file, Pipe file, Symbolic link file, and Socket file
- Linux shell programming - introduction
- Linux shell programming - variables and functions (readonly, unset, and functions)
- Linux shell programming - special shell variables
- Linux shell programming : arrays - three different ways of declaring arrays & looping with $*/$@
- Linux shell programming : operations on array
- Linux shell programming : variables & commands substitution
- Linux shell programming : metacharacters & quotes
- Linux shell programming : input/output redirection & here document
- Linux shell programming : loop control - for, while, break, and break n
- Linux shell programming : string
- Linux shell programming : for-loop
- Linux shell programming : if/elif/else/fi
- Linux shell programming : Test
- Managing User Account - useradd, usermod, and userdel
- Linux Secure Shell (SSH) I : key generation, private key and public key
- Linux Secure Shell (SSH) II : ssh-agent & scp
- Linux Secure Shell (SSH) III : SSH Tunnel as Proxy - Dynamic Port Forwarding (SOCKS Proxy)
- Linux Secure Shell (SSH) IV : Local port forwarding (outgoing ssh tunnel)
- Linux Secure Shell (SSH) V : Reverse SSH Tunnel (remote port forwarding / incoming ssh tunnel) /)
- Linux Processes and Signals
- Linux Drivers 1
- tcpdump
- Linux Debugging using gdb
- Embedded Systems Programming I - Introduction
- Embedded Systems Programming II - gcc ARM Toolchain and Simple Code on Ubuntu/Fedora
- LXC (Linux Container) Install and Run
- Linux IPTables
- Hadoop - 1. Setting up on Ubuntu for Single-Node Cluster
- Hadoop - 2. Runing on Ubuntu for Single-Node Cluster
- ownCloud 7 install
- Ubuntu 14.04 guest on Mac OSX host using VirtualBox I
- Ubuntu 14.04 guest on Mac OSX host using VirtualBox II
- Windows 8 guest on Mac OSX host using VirtualBox I
- Ubuntu Package Management System (apt-get vs dpkg)
- RPM Packaging
- How to Make a Self-Signed SSL Certificate
- Linux Q & A
- DevOps / Sys Admin questions
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization