Skip to content

Latest commit

 

History

History
45 lines (34 loc) · 1.27 KB

10a_redirection.md

File metadata and controls

45 lines (34 loc) · 1.27 KB

Redirect Input and Output

Redirecting Input From a File

Taking the previous example, let's read in data from stdin using file redirection.

Edit the hello_world.sh file using vim to read a file from stdin:

#!/bin/bash
# the read command reads data from stdin
while read name
do
    echo "Hello World, $name!"
done

Create a new file called names.txt with some data like below.

Jenna Pederson
612 Software Foundry

Try this now ex. 5:

BabyMac:~ jennapederson$ ./hello_world.sh < names.txt
Hello World, Jenna Pederson!
Hello World, 612 Software Foundry!

Redirecting Output

Sometimes we don’t want our output to go to the screen, but to a file.

Try this now ex. 6:

BabyMac:~ jennapederson$ ./hello_world.sh < names.txt > output.txt
BabyMac:~ jennapederson$ less output.txt

Notice how we used both input redirection and output redirection here. We are starting to touch on one of the very powerful features of using the command line: stringing commands together.

Note that > will overwrite an existing target file. The alternative output redirection command is to use >>. This will append to the output file.

Back: Connecting Things Together Forward: Piping Commands Together