zsh

Zsh Scripting Lesson: Redirecting the Input and Output in Your Scripts

Welcome to another Zsh scripting lesson. In this one, we will dive into the world of redirection. We will discover all the tools and techniques for redirecting the input and output.

Redirection is a fundamental concept when you need to build functional and powerful scripts as they allow you to manipulate the data streams without needing to write to the disk and more.

Let us dive into the tutorial and explore how redirection works in Zsh and how we can use it in our scripts.

Input and Output Redirection

Redirection refer to the process of managing and manipulating the data streams from the source and to the destination.

As we stated, redirection plays a crucial role in scripting as it allows us to control where the script reads the data and where it can write the result.

By default, Zsh uses the terminal for input and output but we can configure it to accept various more sources and destinations.

Redirecting the Standard Output

The first and most common technique is learning how to redirect the standard output. The standard output is responsible for features such as the output of a command, results from a function, and more.

Let us explore the various streams that you can use to redirect the standard output.

Redirecting to Files

The most common format is redirecting the output to a file. This helps you to quickly save the output of a command into a file that you can reference later from the file system.

In Zsh, we can redirect the standard output to a file using the “>” operator.

An example is as follows:

echo "std out!" > output.txt

Running the given code takes the output from the “echo” command and redirects it to a file called “output.txt”. If the file does not exist in the specified path, it automatically creates it and appends the data from the command.

However, if the file with the specified name exists, it overwrites the contents of the file with the new data.

To prevent overwriting an existing file, you can use the “>>” operator to append the data rather than erasing the existing one.

echo "std out!" >> output.txt

Redirecting to Standard Error

You can also redirect the output to standard error using the “2>” and “2>>” operators, respectively.

An example is as follows:

$ command_that_generates_error 2> error.txt

Running the given command should take the error message and redirect it to the specified file name.

Similarly, you can use the “2>>” operator to append the error to the existing file rather than creating a new one.

Redirecting the Standard Input

Let us now move on from the standard output to the standard input.

Using an Input from Files

In ZSH, we can redirect the standard input from a file using the “<” operator. This loads the data from the specified source into the command on the left hand.

Take the following command demonstration for example:

while read line; do

echo "Line: $line"

done < input.txt

This should read each line from the “input.txt” file and print it out using the “echo” command as defined in the “while” block.

Using Heredocs

Here documents, often denoted as <<EOF, are a convenient way to embed multiple lines of text directly within a shell script.

They allow you to specify a block of text as input to a command without the need for an external file.

An example is as follows:

cat <<EOF

This is

a multiline document.

EOF

In this case, we construct a heredoc using the “cat” command to concatenate and display the content of the files.

The “<<EOF” indicates the start of the here document, and the EOF at the end is used to terminate it. Everything between “<<EOF” and “EOF” is treated as the input to the “cat” command.

One major advantage of using heredocs is the ability to easily insert a multiline text, including variables and special characters, into a script without having to create and manage the temporary files.

Using Pipes

Pipes (|) are a way to redirect the output of one command as the input to another. Take the following command for example:

command1 | command2

In this case, the output of “command1” is treated as the input of the second command. This is very useful when you need to parse the output of a command or file to a pattern matching tools and more.

$ cat info.txt | grep "hello"

In this case, we cat the data from the “info.txt” file and pass it to the “grep” command. This searches for the matching string and returns the result to the terminal.

You can also pair multiple redirections as follows:

cat info.txt | grep "hello" > result.txt

In this case, once grep finds the matching results, instead of printing it out to the standard output, it redirects the result to a file called “result.txt”.

Another example is redirecting the standard output to a file and standard error to another file as demonstrated in the following syntax:

command > output.txt 2> error.txt

You can also redirect both standard output and standard error to the same file.

command > output_and_error.txt 2>&1

Lastly, you can send the input from a file and redirect the output to another file.

command < input.txt > output.txt

Redirecting Both Standard Output and Standard Error

To redirect both the standard output and standard error to the same file, you can use the following syntax:

command > output_and_error.txt 2>&1

Duplicating the File Descriptors

Zsh also allows you to duplicate the file descriptors using “exec” as demonstrated in the following syntax example:

exec 3>&1
command_that_generates_output 1>&3

This should duplicate the standard output to a file descriptor.

Conclusion

In this tutorial, we walked you through everything you need to know when dealing with input/output redirection in Zsh scripting. Stay tuned for more upcoming Zsh scripting tutorials.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list