How can we put our knowledge of command line redirection into practice?

Question

In the lessons on command line redirection, we learned about many functions and operators to chain together to perform progressively more complex tasks. How should we go about effectively using this knowledge in our day-to-day applications?

Answer

To use an analogy, if command line functions are like LEGO pieces, then redirection operators are the various ways that you can connect the pieces. In this way, redirection is crucial to effective use of the command line but as with LEGO, the amount of freedom can be mystifying.

As a result, I may recommend gradually introducing command line use into your daily workflow to solve common problems that come up. Given that command line interfaces and utilities (i.e. functions) have been around for decades, you can be sure that if you have a problem, the command line can solve it using some combination of functions. Let’s take a simple example. Let’s say that I have written a program but now would like to make some changes to variable names. For simplicity, let’s say that I want to change every occurrence of myVar to my_var in a program called prog.c and save it to progNew.c. This can simply be done by opening our editor and running a search a place and then saving. But this can also be done without manually opening the file at all with the following

$ sed -i "s/myVar/my_var/g" prog.c > progNew.c

Such simple applications of the command line and redirection is an easy way to gain confidence and familiarity with its real-world applications.

Beyond this, I cannot recommend highly enough that you read the man pages (manual) for the functions when you’re looking to gain a deeper understanding of how they work. You can find this by searching "man page for ".

1 Like