If you want to make your next Linux interview easier, look no further. Here are some of the top 50 shell script questions that you will most likely be asked in your Linux interviews. The questions are categorized for better skimmability.
Basic Questions Asked in Shell Script Interviews
1. What is a shell?
A shell is a program that takes commands from the keyboard and gives them to the operating system to perform. On most Linux systems, a program called bash (which stands for Bourne Again Shell, an enhanced version of the original Unix shell program, sh, written by Steve Bourne) acts as the command interpreter.
2. What is the difference between a shell and a terminal?
A terminal is a user interface to access an operating system. A shell is a computer program that provides a command line interface for an operating system.
3. What is shell scripting?
A shell script is a computer program designed to be run by the Unix shell, a command-line interpreter. The various dialects of shell scripts are considered to be scripting languages. You may like to read our Shell Scripting 101 series for further information.
4. What is the difference between a shell variable and an environment variable?
A shell variable is a variable that is contained exclusively within the shell in which it was set or defined. An environment variable is an attribute that can be passed from a shell to any child process that it creates.
5. What is the difference between a “subshell” and a “child process”?
A subshell is a separate instance of the shell that is created to execute a particular script. A child process is a process created by another process (the parent process).
6. What is the difference between a soft link and a hard link?
A hard link is a directory entry that points to the inode of a file. A soft link is a directory entry that points to the path of a file.
7. What are the different types of shells available on Linux?
The two primary types of shell are the Bourne Shell (denoted by$) and the C Shell (denoted by %).
The Bourne Shell is further divided into two types:
- BASH: It is an open-source implementation of sh. It stands for Bourne Again Shell.
- KSH: It was initially implemented using a bash scripting shell but was later updated with its advanced properties. KSH stands for Korn Shell.
C Shell also has two subdivisions,
- CSH: It uses a c-programming syntax style, which makes it far more advanced than Bourne Shell. CSH is an abbreviation for “C Shell.”
- TCSH: This is another implementation of CSH, meant to be backwards compatible with CSH. It was a default shell for an operating system called Tenex, hence the letter “t”.
8. What is the difference between $* and $@?
- $* and $@ are used to pass the arguments to the script.
- $* – It treats all the arguments as a single string.
- $@ – It treats all the arguments as separate strings.
9. How do you pass arguments in shell scripting?
The arguments passed to the script are stored in the variables $1, $2, $3, etc. $1 is the first argument, $2 is the second argument, $3 is the third argument, etc.
Also read: Shell Scripting 101: Functions in Shell Scripts
File Management Command Questions
1. What are the file permissions in Linux?
There are three types of file permissions in Linux: read, write, and execute. With read permission, a user can see what’s in a file. With writte permission, a user can change what’s in a file. And with execute permission, a user can run a file as a program. Each file has a set of permissions associated with it that determines which users can read, write, or execute the file.
Also read: Linux chmod Command
2. In a shell, how do you create, read, and delete files?
Create a file:
To create a file, use the touch command. The syntax is as follows:
touch filename.extension
example –
touch test.txt
This will create a file named test.txt in the current directory.
Read a file:
To read a file, use the cat command. The syntax is as follows:
cat filename.extension
example:
cat test.txt
This will display the contents of the file named test.txt in the current directory.
Delete a file:
To delete a file, use the rm command. The syntax is as follows:
rm filename.extension
example:
rm test.txt
This will delete the file named test.txt in the current directory.
3. How to copy and move files in Linux?
To copy a file, use the cp command. The syntax is:
cp source destination
For example, to copy the file /etc/hosts to /tmp/hosts, type:
cp /etc/hosts /tmp/hosts
To move a file, use the mv command. The syntax is:
mv source destination
For example, to move the file /etc/hosts to /tmp/hosts, type:
mv /etc/hosts /tmp/hosts
4. Explain how to change file permissions in Linux?
The chmod command is used to change the permissions of a file. The syntax for chmod is as follows:
chmod [options] mode filename
The mode can be in either symbolic or octal form.
In symbolic form, you can use the following characters to represent different permissions: r (read), w (write), x (execute), and (no permission).
For example, if you want to give read and write permission to a file named myfile, then you would use the following command:
chmod u+rw myfile
In octal form, you can use the following numbers to represent different permissions: 4 (read), 2 (write), and 1 (execute).
For example, if you want to give read and write permission to a file named myfile, then you would use the following command:
chmod 644 myfile
5. Explain how to change file ownership in Linux?
The chown command is used to change the ownership of a file. The syntax for chown is as follows:
chown [options] owner filename
For example, if you want to change the ownership of a file named myfile to user john, then you would use the following command:
chown john myfile
6. how to change file groups in Linux?
The chgrp command is used to change the group of a file. The syntax for chgrp is as follows:
chgrp [options] group filename
For example, if you want to change the group of a file named myfile to group staff, then you would use the following command:
chgrp staff myfile
7. How to create an alias in Linux?
An alias is a command that allows you to create a new name for other commands or sets of commands. The general format for creating an alias is:
alias new-command='command-line'
For example, the following command creates an alias called psa that executes the ps -ef command:
alias psa='ps -ef'
To remove aliases, use the unalias command. For example, to remove the alias you just created, type:
unalias psa
You can also use the alias command to create shorthand versions of commands. For example, the following command creates an alias called c that executes the clear command:
alias c='clear'
You can also use alias to create new commands by combining existing Linux commands. For example, the following command creates an alias called “back” that moves you up one directory level and then lists the contents of that directory:
alias back='cd ..; ls -l'
To make aliases permanent, add them to your .bashrc file in your home directory. The next time you log in, the aliases will be available for use.
8. What are shell functions?
Shell functions are similar to aliases, but they are not quite the same. Shell functions allow you to create new commands by combining existing Linux commands. For example, the following command creates a shell function called “back” that moves you up one directory level and then lists the contents of that directory:
back () { cd ..; ls -l; }
To make shell functions permanent, add them to your .bashrc file in your home directory. The next time you log in, the shell function will be available for use.
Shell functions are more powerful than aliases. For example, shell functions can accept arguments, whereas aliases cannot. Shell functions also allow you to use flow control statements such as if and for in your function definitions.
9. What is mkfifo?
mkfifo is a command that creates a named pipe. A named pipe is a special file type on Linux and Unix-like systems. It allows two or more processes to communicate with each other by reading and writing to and from the pipe.
The mkfifo command takes the name of the FIFO as an argument, creates it, and sets its permissions according to umask.
The default permission for mkfifo is 666 ( -rw-rw-rw- ). You can change this using the -m option followed by octal mode (e.g., 0777 ).
10. What is the difference between a block device and a character device?
A block device is one that can be accessed in blocks of data, such as hard disks or CD-ROMs. A character device is one that can be accessed as a stream of characters, such as terminals or printers. The distinction between the two types of devices has become less important with modern operating systems because they both use buffering to improve performance.
However, it’s still useful for some applications to know whether they are dealing with a stream or blocks of data. For example, if you were writing an application that copied files from one disk to another, you would want it to copy in blocks rather than characters because this would make it much faster (and also more efficient).
You could do this by opening both disks as block devices and then using read() and write() calls on them instead of fread() and fwrite(). This way, your program wouldn’t have any overhead from buffering operations, which would slow down its performance considerably when copying large amounts of data like video files, etc. In addition, there are some devices that can only be accessed as streams of characters, such as terminals or printers.
System utility command questions
1. How should a system service be started, stopped, and restarted in Linux?
Use the systemctl command to start, stop, and restart a service. For example, if you want to restart the httpd service, run:
systemctl restart httpd.service
2. How to enable a service at boot time?
Use the systemctl command to enable a service at boot time.
For example, if you want to enable the httpd service, r
systemctl enable httpd.service
3. How do you find out your Linux server’s disk space utilization information using a command line option?
- df command – Displays file system disk space usage.
- du command – Displays the amount of disk space used by the specified files and subdirectories.
- bdf command – Displays the amount of available free and used disk space on filesystems.
4. What are the commands that can be used to check the Linux server’s CPU utilization?
- top command – Display and update sorted process information.
- mpstat—Displays processor-related statistics.
- sar command—Collect, report, or save information about system activity.
5. How does one find the Linux server’s memory utilization?
- free command – Display the amount of free and used memory in the system.
- vmstat command – Report virtual memory statistics
- top/htop command – Displays linux tasks like the top command but in a more visual manner
6. What are the commands to find the Linux server load average?
- uptime command – Show how long the system has been running.
- top command – Display and update sorted information about processes.
- w command – Show who is logged on and what they are doing.
- last command – Show listing of last logged-in users
7. What are the commands to display the Linux server’s disk IO utilization?
- iostat Command – Report Central Processing Unit (CPU) statistics and input/output statistics for devices, partitions and network filesystems (NFS).
- mpstat Command – Report processor-related statistics
- sar Command – Collect, report, or save system activity information
- pidstat Command – Report statistics for Linux tasks (processes)
8. Which command displays Linux server hardware information?
- dmidecode Command-Dump Desktop Management Interface data
- lshw Command – List Hardware
- lscpu Command – Display Information on CPU Architecture
- lsusb Command-List USB Devices
- lspci Command – List all PCI devices
- hwinfo Command – HardWare Info
Networking command questions
1. What is ifconfig and explain it using examples?
ifconfig (interface configuration) command is used to configure the kernel-resident network interfaces. It is used at the boot time to set up the interfaces as necessary. After that, it is usually used when needed during debugging or when you need system tuning. Also, this command is used to assign the IP address and netmask to an interface or to enable or disable a given interface.
ifconfig [...OPTIONS] [INTERFACE]
2. How can one check the details of a network?
netstat (network statistics) command is used to display network connections, routing tables, interface statistics, masquerade connections, and multicast memberships. It can display the TCP/IP network connections of each protocol. Also, it can display the routing table information on a Linux system.
netstat [OPTIONS] [FILTER]
3. What command can be used to display the IP routes?
The route command is used to show or manipulate the IP routing table in Linux operating systems. This command can be used to add static routes to the destination hosts or networks via an interface after a system reboot also. Also, this command can be used to set the default gateway for a Linux system using the route command with gw option and interface name as eth0.
route [-v] [-FC] {add|del|flush} - Modify routing table for AF.
route {-h|--help}[{add|del|flush}] - Describe usage of these arguments.
route [-nNvee] [-FC] [<AF>] - List kernel routing tables
4 . What is ping?
The ping command is used to check whether a remote host is reachable or not from your machine by sending ICMP echo request packets and waiting for ICMP echo reply packets from that remote host, if it receives any reply packet then it means that the remote host is reachable otherwise not reachable from your machine. This can be done by using the ping utility in Linux operating systems with either the IP address or Hostname of that remote machine as an argument with this ping utility in the terminal window shown below:
$ping [OPTIONS] HOST
5. What is the traceroute command?
traceroute command is used to check the path of packets from your machine to a remote host by sending ICMP echo request packets with TTL value 1 and waiting for ICMP time to exceed the reply packet from the first hop router, then it sends another ICMP echo request packet with TTL value 2 and waits for an ICMP time exceeded reply packet from second hop router and so on until it receives an ICMP echo reply packet from that remote host.
This can be done by using the traceroute utility in Linux operating systems with either the IP address or Hostname of that remote machine as an argument with this traceroute utility in the terminal window like shown below:
$traceroute [OPTIONS] HOST
6 . How can we query the details of a DNS server?
dig command is used to query DNS servers about any domain name information like A record (IP address), MX record (Mail server), CNAME record (Canonical Name), etc.
This can be done by using the dig utility in Linux operating systems with either the IP address or Hostname of that remote machine as an argument with this dig utility in the terminal window as shown below:
$dig [@server] [-b addr] [-c class] [-f filename] [-k filename] [-m] [-p port#] [-t type ] [domain ] [q-type ] [q-class ] {q-opt } {global-d-opt } host
7. What is nslookup?
nslookup command is used to query DNS servers about any domain name information like A records (IP address), MX records (Mail Servers), CNAME records (Canonical Names), etc.
$nslookup [OPTIONS] [HOSTNAME]
8 . What is wget?
wget command is used to download files over the network using HTTP, HTTPS and FTP protocols. This can be done by using wget utility in Linux operating systems with either URL of that file as an argument with this wget utility in the terminal window like shown below:
$wget [OPTIONS]... [URL]...
9 . What is curl and explain it using examples?
The curl command is a command line utility that allows data to be transferred across many network protocols. It communicates with a web or application server by supplying a relevant URL and the data to be transferred or received.
$curl -O http://www.example.com/file[1-100].jpg
10 . How can a person connect to a specific port of a machine?
telnet command is used to connect to a remote machine on a specific port using telnet utility in Linux operating systems with either IP address or Hostname of that remote machine as an argument with this telnet utility in the terminal window like shown below:
$telnet [OPTIONS] HOST [PORT]
Text Processing and scripting commands
1. What is grep?
grep searches the input files for lines containing a match to a given pattern list. When it finds a match in a line, it copies the line to standard output (by default), or another output you have requested with options.
By default, grep prints the matching lines. In addition, two variant programs egrep and fgrep are available.
Egrep is the same as grep -E . Fgrep is the same as grep -F .
Grep understands two different versions of regular expression syntax: “basic” and “extended”.
In GNU grep, there is no difference in available functionality using either syntax; basic regular expressions are less powerful than extended ones but typically run faster because they require less processing by the regexp engine when searching for matches while parsing text input data stream(s).
grep [OPTIONS] PATTERN [FILE...]
4. What is sed command and give the syntax of the command?
sed (stream editor) is a Unix utility that parses and transforms text, using a simple, compact programming language. sed was developed from 1973 to 1974 by Lee E. McMahon of Bell Labs, and is available today for most operating systems
sed [OPTIONS]... {script-only-if-no-other-script} [input-file]...
5. What is a cut command?
The cut command in UNIX is a command for cutting out the sections from each line of files and writing the result to standard output. It can be used to cut parts of a line by byte position or character; it can also be used to cut a rectangular section out of input (similar to a column in Unix).
cut -c list filename(s)
6. What is a sort command?
The sort command sorts lines of text files according to one or more criteria specified on the command line. By default, sort reads from standard input (stdin),
sort [OPTION]... [FILE]...
7. What is uniq Command?
The uniq utility reads the given input_file, compares adjacent lines, and writes a copy of each unique input line to the output_file.
uniq [OPTION]... [INPUT[OUTPUT]]
8. What is head Command?
Output the first part of the file(s). The head command prints the first 10 lines of each FILE to standard output. With more than one file, precede each with a header giving the file name. With no FILE, or when FILE is -, read standard input.
head [-n count | -c bytes] [file ...]
9. What is tail Command?
Output the last part of the file(s). Tail prints the last 10 lines (or any number specified by option -n) of each file to standard output. With more than one FILE, precede each with a header giving the file name. With no FILE, or when FILE is -, read standard input.
tail [-F | -f | -r] [-q] [-b number | -c number | -n number] [file ...]
10. Explain the wc (word count) Command?
Print newline, word, and byte count for each file. The wc command displays the number of lines, words, and bytes contained in each input file, or standard input (if no file is specified) to the standard output.
wc [OPTION]... [FILE]...
12. What is tee Command?
Read from standard input and write to standard output and files. The tee command copies standard input to standard output, making a copy in zero or more files. The output is unbuffered.
tee [OPTION]... [FILE]...
Summary
Shell Scripting is an extremely versatile tool that can be used for a variety of purposes. Shell scripting can be used to automate tasks, create scripts to manage systems, or even just to improve productivity.
Shell scripting can be a daunting task at first, but with some practice it can become quite easy. Shell scripting is a great way to get started with linux and automation, and it has many potential uses in the IT world.