Linux Questions & Answers – Shell Programming

This set of Linux / Unix questions and answers focuses on Bash Shell programming. It will be useful for both fresher & experienced who are learning Linux Basics, Essentials and Shell programming.

1. What will be output of following command:

$ echo "The process id is" $$$$
 
a) The process id is $$
b) The process id is $<pid>$<pid>
c) The process id is <pid><pid>
d) The process id is $$$$
View Answer
Answer:c

2. What would be the current working directory at the end of the following command sequence?

 $ pwd
 /home/user1/proj
 $ cd  src
 $ cd  generic
 $ cd  .
 $ pwd

a) /home/user1/proj
b) /home/user1/proj/src
c) /home/user1
d) /home/user1/proj/src/generic
View Answer

Answer:d

3. How do you print the lines between 5 and 10, both inclusive
a) cat filename | head | tail -6
b) cat filename | head | tail -5
c) cat filename | tail +5 | head
d) cat filename | tail -5 | head -10
View Answer

Answer:a

4. Create a new file “new.txt” that is a concatenation of “file1.txt” and “file2.txt”
a) cp file.txt file2.txt new.txt
b) cat file1.txt file2.txt > new.txt
c) mv file[12].txt new.txt
d) ls file1.txt file2.txt | new.txt
View Answer

Answer:b

5. which of these is NOT a valid variable in bash
a) __ (double underscore)
b) _1var (underscore 1 var )
c) _var_ (underscore var underscore)
d) some-var (some hyphen var)
View Answer

Answer:d

6. What is the output of the following code:

os=Unix
echo 1.$os 2."$os" 3.'$os' 4.$os
 
a) 1.Unix 2.Unix 3.Unix 4.Unix
b) 1.Unix 2.Unix 3.$os 4.Unix
c) 1.Unix 2.Unix 3.Unix 4.$os
d) 1.Unix 2.$os 3.$os 4.$os
View Answer
Answer:b

7. What is the return value ($?) of this code:

os = Unix
[$osName = UnixName] && exit 2
[${os}Name = UnixName] && exit 3
 
a) 0
b) 1
c) 2
d) 3
View Answer
Answer:d

8. What is the output of the following program?

x = 3; y = 5; z = 10;
if [( $x -eq 3 ) -a ( $y -eq 5 -o  $z -eq 10 )]
then
    echo $x
else
    echo $y
fi

a) 1
b) 3
c) 5
d) Error
View Answer

Answer:b

9. What is the output of the following program?

[ -n $HOME ]
echo $?
[ -z $HOME ]
echo $?
a) 0
   1
b) 1
   0
c) 0
   0
d) 1
   1
View Answer
Answer:a

10. What is the output of the following program?

b = 
[ -n $b ]
    echo $?
[ -z $b ]
    echo $?
a) 1
   1
b) 2
   2
c) 0
   0
d) 0
   1
View Answer
Answer:c

11. The expression expr -9 % 2 evaluates to:
a) 0
b) 1
c) -1
d) 2
View Answer

Answer:c

12. The statement z = ‘expr 5 / 2′ would store which of the following values in z?
a) 0
b) 1
c) 2
d) 2.5
e) 3
View Answer

Answer:c

Sanfoundry Global Education & Learning Series – Linux Administration & Programming.

Here’s the list of Best Reference Books in Linux Commands & Shell Programming.
Here’s the list of Best Reference Books in Linux Kernel, Device-Drivers & System Programming.

To practice all questions on Linux Administration & Programming, here is complete set of 1000+ Multiple Choice Questions and Answers on Linux.

Subscribe Newsletter & Posts
About

Manish Bhojasia, a technology veteran with 17+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below.


Sanfoundry Training Center of Excellence (CoE)

Manish Bhojasia, a technology veteran with 17+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below.







1000 Linux MCQs | Quiz

Linux Environment - 1
Linux Environment - 2
Linux Environment - 3
Linux Commands - 1
Linux Commands - 2
Linux Commands - 3
Linux Commands - 4
Linux File Management - 1
Linux File Management - 2
Linux File Types
Linux File Permissions - 1
Linux File Permissions - 2
Linux File System Overview
Linux Startup and Shutdown
Linux Process Management
Linux User Account Management
Linux Shell Programming
Linux Shell Environment - 1
Linux Shell Environment - 2
Linux Shell Redirection
Linux Shell Special Symbols
Linux Search Pattern
Linux Shell Functions
Linux Shell Variables
Linux Bash Arithmetic Expressions
Linux Bash Command History & Job Control
Linux Bash Built-in Commands - 1
Linux Bash Built-in Commands - 2
Linux Bash Built-in Commands - 3
Linux vi Editor
Linux sed Editor
Awk Programming Basics
Awk Programming Expressions
Awk Programming Control Statements
Awk Programming Varaibles and arrays
Linux Filesystem Hierarchy - 1
Linux Filesystem Hierarchy - 2
Linux Proc Filesystem - 1
Linux Proc Filesystem - 2
Linux Proc Filesystem - 3
Linux Proc Filesystem - 4
Linux Proc Filesystem - 5
Makefile Questions & Answers - 1
Makefile Questions & Answers - 2
GCC Compiler Various Options - 1
GCC Compiler Various Options - 2
GCC Compiler Various Options - 3
GCC Compiler - Stages of Compilation - 1
GCC Compiler - Stages of Compilation - 2
Static Libraries Questions & Answers
Shared Libraries Questions & Answers
GDB Debugger Questions & Answers - 1
GDB Debugger Questions & Answers - 2
GDB Debugger Questions & Answers - 3
GDB Debugger Questions & Answers - 4
GDB Debugger Questions & Answers - 5
Linux Sysfs Questions & Answers - 1
Linux Sysfs Questions & Answers - 2
Linux Sysfs Questions & Answers - 3
Linux Sysfs Qusetions & Answers - 4
Linux Sysfs Questions & Answers - 5
Device Drivers Major-Minor Numbers Questions
Linux Process Management
Linux Memory Management
Linux File Management - 1
Linux File Management - 2
Linux Signal Handling
Linux IPCs - 1
Linux IPCs - 2
Linux Systems
Memory Allocator Debugging Questions - malloc, calloc, free and realloc Calls - 1
Memory Allocator Debugging Questions - malloc, calloc, free and realloc Calls - 2
File Handling Debugging Questions - dup, fcntl, lseek and read System Calls
Process Management Debugging Questions - fork, exec and wait System Calls
Debugging Questions - Signal Handling System Calls
System Resource Debugging Questions - Timer, User & Resource Limit System Calls
Debugging Questions - Posix Threads
Debugging Questions - PThreads Handling
Debugging Questions - Named and Un-named Pipe Calls
Debugging Questions - System-V IPCs - Message Queues, Shared Memory and Semaphores
Debugging Questions - POSIX IPCs - Message Queues, Shared Memory and Semaphores
Debugging Questions - Unix Domain Sockets
Debugging Questions - Internet Domain Socket System Calls
Subscribe Newsletter & Posts
Follow Manish & Sanfoundry