Linux redirected output not showing in file
Recently, when working with Nginx, I wanted to export out the details of my Nginx build so I could see all modules the webserver was compiled with. ย However, each time I tried to redirect the output of theย nginx -Vย command, the linux redirected output not showing in the file. ย I would see the command output on the screen, the file I was redirecting to was created, but nothing was being added to the file. ย The command I was using was: ย nginx -V >> output.txt
The terminal never complained about syntax issues with the command, however, just wasn’t appending any data to the file. ย After a search session on Google, I found the error of my ways. ย The command needed to be structured: ย nginx -V &> output.txt
The “&” addition to the command syntax, tells the terminal processor to redirectย both standard outputย andย standard error outputย to the file. ย If you don’t redirect both with some commands, the standard output is simply displayed on screen and the file is never used for output even though it is created. ย Other commands such asย ls ย however work as expected without the “&” in the command string.
If you run into an issue where you are needing to capture the output of a command in the terminal in Linux and can’t seem to get the command to output correctly to the file, most likely, you have an issue where you need to redirect the standard and error output both for the command to work. ย The symptoms also can be the command output is displayed on the screen if not redirected correctly, and not displayed on the screen when it does redirect correctly.
Take a look at the screenshot below for the results of the command that was executed and redirected properly. ย Notice that we get only the command and returned back to a terminal prompt.