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:
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.
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:
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:
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:
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:
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.
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:
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:
You can also redirect both standard output and standard error to the same file.
Lastly, you can send the input from a file and redirect the output to another file.
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:
Duplicating the File Descriptors
Zsh also allows you to duplicate the file descriptors using “exec” as demonstrated in the following syntax example:
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.