Shell Redirection Questions & Answers

Shell Redirection

Linux Questions & Answers – Shell Redirection

This set of Linux / Unix interview questions and answers focuses on Bash Shell programming & administration. It will be useful for both fresher & experienced preparing interviews on Linux Basics, Essentials and Shell programming.

1. Which of these is the correct method for appending “foo” in /tmp/bar file?
a) echo foo > /tmp/bar
b) echo foo >> /tmp/bar
c) echo foo | /tmp/var
d) /tmp/bar < echo foo

2. Syntax to suppress the display of command error to monitor?
a) command > &2
b) command 2> &1
c) command 2> &2
d) command 2> /dev/null

3. The following commands gives the output like this

    #cat file1 file2
    #cat: file1: No such file or directory
    hello
    If we execute the command “cat file1 file2  1>2  2>&1” the output would be

a) cat: file1: No such file or directory hello
b) No output is displayed
c) Cat: 1>2: No such file or directory
d) hello

4. cat < file1 >> file2 | file3
a) file1 content will be appended to file2 and finally stored in file3
b) file1 content will be appended to file2 and file3 will be ignored
c) file2 and file3 will have same content
d) syntax error

5. Executing cat /etc/password > /dev/sda as superuser will
a) Write data into a regular file called /dev/sda
b) Write data to the physical device sda
c) Create a temporary file /dev/sda and write data to it
d) None of the mentioned

6. From where would the read statement read if the following statements were executed?

   exec < file1
   exec < file2
   exec < file3
   read line

a) It would read all the files
b) It would not read any files
c) It would read all the files in reverse order
d) It would read only file3

7. The redirection 2> abc implies
a) Write file 2 to file abc
b) Write standard output to abc
c) Write standard error to abc
d) None of the mentioned

8. cmd 2>&1 > abc will
a) Write file2 to file1
b) Write standard output and standard error to abc
c) Write standard error to abc
d) Write standard output to abc & standard error to monitor

9. cmd > abc 2>&1 will
a) Write file2 to file1
b) Write standard output and standard error to abc
c) Write standard error to abc
d) Write standard output to abc & standard error to monitor

The file handles of commands can be duplicated, opened, closed, made to refer to different files, and the files the command reads from and writes to can be changed using redirection. In the present shell execution context, redirection can also be used to modify file handles. Redirection is a Linux feature that allows you to change the normal input/output devices when running a command. Any Linux command’s core operation is to receive an input and produce something. The keyboard is the standard input (stdin) device. The screen is the standard output (stdout) device.

Leave a Reply

Your email address will not be published.