Linux Shell Scripting

Linux Shell Scripting Question and answers that can help you to prepare for different Linux Interviews

Q.1 According to you, what is the need to connect the system to remote server, in UNIX shell scripting?
The need to connect the system to remote server, in UNIX shell scripting is when it comes to executing some special commands and the users are free to get this done through various commands. This helps the users to get an additional control or to get some additional support or help. But the approach has to be secured with an authentication process which can be a simple username and a password.
Q.2 What is the function of utilities that come with the open client driver in the shell scripting?
They are considered useful for connecting the system with a database server simply. Such that the users have no performed this task to accomplish so many tasks that are relevant and important in scripting. Since scripting requires a lot of data and information and it are not always possible that the same is kept at a particular location only. Here the users are required to ensure of an error-free outcome in this approach.
Q.3 What do you understand by MBR in the shell scripting and why is it considered useful for the users?
MBR is abbreviated as Master Boot Record which is a small program whose prime function is to ensure that the kernel is loaded during the system boot set up. It is present in the boot block. The MBR is considered useful because it makes sure that the users can perform the boot looping tasks accurately and reliably.
Q.4 Let us suppose you have been asked execute a command using exec, then what will be the status of your current process in the shell?
Given the above scenario, all the forked processes which are new get overlays when the 'exec' is executed. This command simply gets executed without making any impact on the current process. Such that no new process will be created in this scenario.
Q.5 What is a shell script in Linux?
A shell script is a program written in a shell language, such as Bash, that contains a sequence of commands to be executed.
Q.6 What is the purpose of the shebang (#!) in a script?
The shebang specifies the interpreter to be used for executing the script. For example, #!/bin/bash indicates Bash.
Q.7 How do you create a basic shell script file?
You can create a shell script by using a text editor to write the script's commands and saving it with a .sh extension.
Q.8 What is the difference between single and double quotes in shell scripting?
Single quotes preserve the literal value of all characters within them, while double quotes allow variable and command substitution.
Q.9 How can you make a shell script executable?
Use the chmod +x command to make a script executable, e.g., chmod +x myscript.sh.
Q.10 What is a comment in a shell script, and how is it added?
A comment is a non-executable part of a script used for documentation. It's added with the # symbol, e.g., # This is a comment.
Q.11 How do you run a shell script?
You can run a shell script by executing it with the ./ prefix, e.g., ./myscript.sh, or by specifying the shell explicitly, e.g., bash myscript.sh.
Q.12 What is variable interpolation in shell scripting?
Variable interpolation allows you to include the value of a variable within a string using $var or ${var} notation.
Q.13 What are environment variables, and how are they different from shell variables?
Environment variables are accessible by all processes and can be set using the export command, while shell variables are local to the current shell session.
Q.14 How do you assign a value to a variable in a shell script?
Use the assignment operator = to assign a value to a variable, e.g., myvar="Hello".
Q.15 How do you print the value of a variable in a script?
To print the value of a variable, use echo $var, where $var represents the variable name.
Q.16 What is command substitution in shell scripting?
Command substitution allows you to execute a command and use its output as part of another command. It's done using $(command) or backticks, e.g., result=$(date).
Q.17 What are arithmetic operations in shell scripting?
You can perform arithmetic operations using expr, $(( )), or let. For example, result=$((5 + 3)).
Q.18 How do you read input from a user in a shell script?
Use the read command to read input from the user and store it in a variable, e.g., read username.
Q.19 What are conditional statements in shell scripting?
Conditional statements, like if, elif, and else, allow you to execute code based on specific conditions.
Q.20 How do you check if a file exists in a shell script?
Use the -e option with the test command or [ -e file ] to check if a file exists.
Q.21 What is the purpose of the case statement in shell scripting?
The case statement allows you to perform multiple conditional checks in a more structured way than if-elif-else.
Q.22 How do you create a loop in shell scripting?
You can use for, while, and until loops to repeat commands or iterate through lists of items.
Q.23 How do you exit a shell script prematurely?
Use the exit command followed by an exit status code to exit a script, e.g., exit 0 for success or exit 1 for failure.
Q.24 What is the purpose of the break and continue statements in loops?
break exits a loop prematurely, while continue skips the current iteration and proceeds to the next.
Q.25 What are command-line arguments in shell scripting?
Command-line arguments are values passed to a script when it's executed, accessible as $1, $2, and so on.
Q.26 How do you check the number of arguments passed to a script?
Use $# to get the count of arguments passed to a script.
Q.27 What is the role of the shift command in shell scripting?
shift shifts the values of command-line arguments, allowing you to access the next argument with $1.
Q.28 How do you use wildcards (e.g., * and ?) in shell scripting?
Wildcards are used for pattern matching, allowing you to work with groups of files that match a pattern.
Q.29 What is the purpose of the grep command in shell scripting?
grep is used for searching and matching patterns in text files, and it's often used with regular expressions.
Q.30 How do you redirect output in shell scripting?
You can use > for output redirection and >> to append output to a file. For example, echo "Hello" > output.txt.
Q.31 What is piping in shell scripting, and how is it used?
Piping (`
Q.32 What is a subshell in shell scripting?
A subshell is a child shell created within the parent shell, typically used for isolating variables and commands.
Q.33 How do you set and unset environment variables in a script?
Use the export command to set environment variables and the unset command to remove them.
Q.34 What is a shell function, and how do you define one?
A shell function is a reusable block of code defined with the function keyword or as name() { ... }.
Q.35 How do you call a shell function within a script?
To call a function, simply use its name followed by parentheses, e.g., myfunction().
Q.36 How can you pass arguments to a shell function?
Arguments are passed to a function in the same way as command-line arguments, e.g., $1, $2, and so on.
Q.37 What is recursion, and can you use it in shell scripts?
Recursion is a technique where a function calls itself. Yes, you can use recursion in shell scripts.
Q.38 How do you debug a shell script?
You can use set -x for debugging to display each command as it's executed.
Q.39 What is a shell script's exit status code, and how is it accessed?
The exit status code is a numeric value indicating the script's success or failure. You can access it using $?.
Q.40 How do you check if a process is running in a shell script?
You can use commands like ps and pgrep to check if a process is running.
Q.41 What is the purpose of the trap command in shell scripting?
trap allows you to specify actions to be taken when certain signals are received, such as cleaning up before script termination.
Q.42 How can you pause a script and wait for user input?
Use the read command without specifying a variable to pause and wait for user input.
Q.43 What is a symbolic link (symlink) in Linux, and how do you create one?
A symlink is a reference to another file or directory. You can create one with the ln -s command, e.g., ln -s source_file symlink.
Q.44 How do you check if a file is a regular file or a directory in a shell script?
Use the test command with -f to check if it's a regular file or -d to check if it's a directory.
Q.45 What is the purpose of the dirname and basename commands in shell scripting?
dirname extracts the directory path from a file path, and basename extracts the filename from a path.
Q.46 How do you schedule tasks to run at specific times using cron?
Use the crontab command to create and manage cron jobs, specifying the schedule and the command to run.
Q.47 What is the purpose of the at command in shell scripting?
The at command allows you to schedule one-time tasks to run at a specified time.
Q.48 How do you find the size of a file in a shell script?
Use the du or stat command to determine the size of a file.
Q.49 What is the difference between a hard link and a symbolic link?
A hard link points directly to the file's data blocks, while a symbolic link is a separate file that references another file's path.
Q.50 How do you compare strings in a shell script?
You can use operators like == and != or the test command with = and != to compare strings.
Q.51 What is the purpose of the cut command in shell scripting?
The cut command is used to extract specific columns or fields from text data.
Q.52 How do you find and replace text in a file using a shell script?
You can use tools like sed or awk to find and replace text in a file.
Q.53 What is the purpose of the awk command in shell scripting?
awk is a text processing tool used to manipulate and analyze text data, often in tabular form.
Q.54 How do you count the number of lines, words, or characters in a file using shell commands?
Commands like wc -l count lines, wc -w counts words, and wc -c counts characters in a file.
Q.55 How do you list files in a directory using a shell script?
Use the ls command with appropriate options like -l, -a, or -R to list files in a directory.
Q.56 What is the role of the find command in shell scripting?
find is used to search for files and directories based on various criteria, such as name, type, or size.
Q.57 How do you find and delete files older than a specific date in a shell script?
You can use find with the -mtime option to locate and delete files older than a certain number of days.
Q.58 How do you create a temporary file in a shell script?
You can use the mktemp command to create a temporary file with a unique name.
Q.59 What is the purpose of the sort command in shell scripting?
sort is used to sort lines of text in a file or standard input.
Q.60 How do you calculate the length of a string in a shell script?
Use the ${#string} notation to calculate the length of a string, e.g., ${#mystring}.
Q.61 What is the purpose of the declare and typeset commands in shell scripting?
These commands are used to declare and set attributes for variables, such as making them read-only or integer types.
Q.62 How do you perform string concatenation in shell scripting?
You can concatenate strings using the + operator or simply by placing them together, e.g., result="$string1$string2".
Q.63 What is process substitution, and how is it used in shell scripting?
Process substitution allows you to treat the output of a command as a file, often used with commands like diff and sort.
Q.64 How do you generate random numbers in a shell script?
Use the $RANDOM variable to generate random numbers, e.g., randnum=$((RANDOM % 100)).
Q.65 How do you capture the output of a command into a variable in a shell script?
Use command substitution with $(command) or backticks, e.g., result=$(date).
Q.66 How do you check if a string contains a substring in shell scripting?
Use the [[ $string == *substring* ]] construct to check for substring existence.
Q.67 What is the difference between a local and global variable in a shell script?
Local variables are confined to a specific function or scope, while global variables are accessible throughout the script.
Q.68 How do you handle errors and exceptions in a shell script?
You can use conditional statements and exit codes to handle errors, along with error handling functions.
Q.69 How do you perform floating-point arithmetic in shell scripting?
Shell scripting typically handles integers, but you can use external tools like bc for floating-point arithmetic.
Q.70 How do you check if a directory exists in shell scripting?
Use the -d option with the test command or [ -d directory ] to check if a directory exists.
Q.71 How do you execute a command as a different user in a shell script?
You can use sudo to execute a command as a different user, provided you have the necessary permissions.
Q.72 What is a here document in shell scripting, and how is it used?
A here document is a way to include multi-line text in a script or command. It's defined using << followed by a delimiter.
Q.73 How do you find the number of arguments passed to a script in shell scripting?
Use $# to get the count of arguments passed to a script.
Q.74 What is a regular expression (regex), and how is it used in shell scripting?
A regex is a pattern-matching expression used to search and manipulate text data. It's commonly used with tools like grep and sed.
Q.75 How do you rename files in bulk using a shell script?
You can use a loop and the mv command to rename files in bulk, specifying a pattern for renaming.
Q.76 How do you create a symbolic link (symlink) in shell scripting?
Use the ln -s command followed by the source and target paths, e.g., ln -s source_file symlink.
Q.77 How do you check if a string is empty in shell scripting?
Use the -z option with the test command or [ -z "$string" ] to check if a string is empty.
Q.78 What is the purpose of the tee command in shell scripting?
tee is used to redirect output to a file while also displaying it on the terminal.
Q.79 How do you get the current date and time in a shell script?
Use the date command with appropriate format options to obtain the current date and time.
Q.80 How do you read lines from a file in a shell script?
You can use a while loop with read to read lines from a file, e.g., while read line; do ... done < file.txt.
Q.81 How do you compare integers in shell scripting?
You can use operators like -eq, -ne, -lt, -le, -gt, and -ge to compare integers.
Q.82 What is the purpose of the eval command in shell scripting?
eval is used to evaluate and execute dynamically generated shell commands stored in a string.
Q.83 How do you list only directories in a directory using shell commands?
Use the find command with the -type d option to list only directories within a directory.
Q.84 What is the purpose of the timeout command in shell scripting?
timeout is used to run a command with a specified time limit, terminating it if it exceeds the limit.
Q.85 How do you use the case statement to match patterns in shell scripting?
The case statement is used to compare a value against a set of patterns and execute code based on the matching pattern.
Q.86 How do you convert a string to uppercase or lowercase in shell scripting?
You can use the tr command or parameter expansion ${var^^} for uppercase and ${var,,} for lowercase conversion.
Q.87 How do you compare two files in shell scripting?
You can use tools like diff, cmp, or md5sum to compare the contents of two files.
Q.88 What is the purpose of the source or . command in shell scripting?
source or . is used to execute commands from a script within the current shell environment, affecting the current session.
Q.89 How do you remove duplicates from a file in shell scripting?
You can use sort with the -u option or awk to remove duplicate lines from a file.
Q.90 What is the difference between /dev/null and /dev/zero in shell scripting?
/dev/null discards output, while /dev/zero generates an infinite stream of null bytes.
Q.91 How do you check if a process is running in shell scripting?
You can use commands like ps, pgrep, or pidof to check if a process is running.
Q.92 What is the purpose of the readonly command in shell scripting?
readonly is used to make a variable read-only, preventing its value from being changed.
Q.93 How do you read a specific line from a file in shell scripting?
You can use tools like sed or awk to read a specific line from a file based on line numbers or patterns.
Q.94 What is the difference between a shell and a terminal in Linux?
A shell is a command interpreter, while a terminal is the user interface for interacting with the shell.
Q.95 How do you create a backup of a file in shell scripting?
You can use the cp command to create a backup copy of a file, e.g., cp file.txt file_backup.txt.
Q.96 How do you list files in a directory recursively in shell scripting?
Use the find command with the -type f option to list files recursively.
Q.97 What is the purpose of the ps command in shell scripting?
ps is used to display information about running processes on the system.
Q.98 How do you use the awk command to extract specific fields from text data?
awk uses field separators to extract specific fields, e.g., awk -F',' '{print $2}' extracts the second field separated by commas.
Q.99 What is the role of the basename command in shell scripting?
basename extracts the filename from a path, ignoring the directory portion.
Q.100 How do you list files with a specific extension in a directory using shell commands?
You can use find with the -name option to list files with a specific extension, e.g., find /path -name "*.txt".
Q.101 How do you calculate the length of an array in shell scripting?
Use ${#array[@]} to calculate the length of an array.
Q.102 How do you append to a file without overwriting its content in shell scripting?
Use the >> operator to append output to a file without overwriting its content.
Q.103 What is the purpose of the basename command in shell scripting?
basename extracts the filename from a path, ignoring the directory portion.
Q.104 How do you create a directory in shell scripting?
Use the mkdir command to create a directory, e.g., mkdir mydir.
Q.105 What is the role of the dirname command in shell scripting?
dirname extracts the directory path from a file path.
Q.106 How do you extract a specific column from a CSV file using shell commands?
You can use cut or awk to extract a specific column from a CSV file by specifying the delimiter.
Q.107 How do you find and replace text in multiple files using shell scripting?
Use tools like sed or a combination of find and grep to find and replace text in multiple files.
Q.108 How do you check if a file is empty in shell scripting?
You can use the -s option with the test command or [ -s file ] to check if a file is not empty.
Q.109 What is the purpose of the select statement in shell scripting?
The select statement is used to create interactive menus in shell scripts.
Q.110 How do you create a backup of a directory in shell scripting?
You can use the tar command to create a backup (tarball) of a directory, e.g., tar -cvzf backup.tar.gz directory.
Q.111 How do you define a shell?
Shell is a defined interface between the user and the kernel. Although there can be only one kernel; a system can have many shell running simultaneously. Therefore when a user enters a command through the keyboard, the shell communicates with the kernel to execute it and then display the output to the user.
Q.112 In a Linux system, what are the different kinds of commonly used shells?
The different kinds of commonly used shells are csh,ksh,bash,Bourne . One opf the most commonly used and advanced shell used today is "Bash" .
Q.113 Differentiate between soft and hard links.
The key point of difference between soft and hard links are - The soft links are link to the file name and can reside on different filesytem as well; on the other hand, hard links are link to the inode of the file and have to be on the same filesytem as that of the file. Such that deleting the original file makes the soft link inactive (broken link) but does not affect the hard link (Hard link will still access a copy of the file)
Q.114 What is the importance of $#?
$# represents the count of the arguments passed to the script.
Q.115 Differentiate between $* and $@
$@ considers each quoted arguments as separate arguments whereas $* will consider the entire set of positional parameters as a single string
Q.116 What do you understand by zombie processes?
Zombie processes are the processes that have died but there exit status is still not picked by the parent process. Zombies processes even if not functional still have its process ID entry in the process table.
Q.117 What do you understand by shell in terms of scripting?
We can define shell in terms of scripting as a direct link among the kernel and the user. Such that it is possible for the system to run multiple shells at the same time irrespective of the fact that there is always only one kernel that remains present. Therefore when a command is given by the user, shell begins exchanging information with the kernel, wherein the operations are then executed and the information is conveyed to the user.

Some of the commonly used shells in scripting are ksh, bourne,bash, csh
Q.118 How can you create shortcut in the Linux?
We can create shortcuts with the help of links present in the UNIX. There are two links which are considered which are generally categorized as,
1. Soft Link
2. Hard Link
Q.119 Give a brief about the Super Block in Shell scripting.
Super Block in Shell scripting is basically a program containing all the information with reference to a specific file system. Super Block reflects the block size that is used by its associated number, the size of system in terms of data handling and programming. This also provides information regarding the free inodes and the data blocks which are currently associated with the system.
Q.120 What is the possibility to pass arguments to the scripts when the same in running in the UNIX?
It is possible to pass arguments to the scripts ehile running in UNIX such that it is followed often. This is done to ensure that the system does not have any error while executing the command and also the scripts are running smoothly.
Q.121 What are the crucial standard streams in the UNIX shell scripting?
The crucial standard streams in the UNIX shell scripting are Standard Input, Standard Output as well as Standard Error
Q.122 How can we calculate the overall number of arguments which are passed to a script and why is it important?
We can calculate the overall number of arguments that are passed to a script by a command $#. Also the same is required to know the overall load on a script. Also every limit has a limit on handling the same and users have to check it often to ensure script remains in the running mode and does not get fail.
Q.123 What will you do to treat the quoted arguments as separate ones from the general?
We can treat the quoted arguments as separate ones from the general using the command $@. It is possible to treat them as a single entity using the command $* can be applied directly.
Q.124 How can you display the process ID of the process executing currently and ID of processes that take place recently, in shell scripting?
We can display the process ID of the process executing currently and ID of processes that take place recently, in shell scripting by using two dedicated commands that can be executed directly and they are namely, $$ and $.
Q.125 What is the importance of Shebang line, in shell scripting, ?
Shebang line provides information regarding the location where the engine is placed such that the engine is the one that execute the script. Shebang line is present at the top of the script and it can be ignored by the users if they want to.
Get Govt. Certified Take Test