0% found this document useful (0 votes)
9 views8 pages

Shell Script Interview Q & A

The document provides a comprehensive guide on foundational shell scripting concepts, covering variable creation, command execution, loops, and error handling. It includes examples for creating functions, using conditionals, and manipulating files, among other topics. Key commands and their usage, such as echo, read, and sed, are also explained.

Uploaded by

Charan Sunny
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views8 pages

Shell Script Interview Q & A

The document provides a comprehensive guide on foundational shell scripting concepts, covering variable creation, command execution, loops, and error handling. It includes examples for creating functions, using conditionals, and manipulating files, among other topics. Key commands and their usage, such as echo, read, and sed, are also explained.

Uploaded by

Charan Sunny
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Foundational shell scripting knowledge part2:

31. How do you create a variable in a shell script?


• Answer: You can create a variable by simply assigning a value to it using
= without spaces. For example, name="John".

32. How can you print a variable's value?


• Answer: Use the echo command. For example, echo $name will print
the value of the variable name.

33. What is the purpose of the read command?


• Answer: The read command is used to take user input. For example,
read username stores user input in the variable username.

34. How do you create a comment in a shell script?


• Answer: Comments are created using #. Anything following # on the line
will be ignored by the shell.

35. What is a positional parameter?


• Answer: Positional parameters are variables like $1, $2, $3, etc., that
hold command-line arguments passed to the script.

36. How do you check if a file exists in a shell script?


• Answer: Use the -e option with an if statement. Example: if [ -e
filename ]; then echo "File exists"; fi.

37. How can you append text to a file?


• Answer: Use the >> operator. For example, echo "text" >> filename
appends "text" to filename.

38. What is the basename command used for?


• Answer: basename strips directory and suffix from filenames. Example:
basename /path/to/[Link] returns [Link].

39. How do you create a function in a shell script?


• Answer: Define a function using:
• my_function() {
echo "This is a function"
}

1
40. What is the use of exit in a shell script?
• Answer: The exit command terminates the script and returns an exit
status.

41. How do you execute a shell script?


• Answer: Make it executable using chmod +x [Link], then run it with
./[Link].

42. How do you redirect standard error to a file?


• Answer: Use 2>. For example, command 2> [Link] redirects standard
error to [Link].

43. How do you redirect both standard output and standard error?
• Answer: Use &> or 2>&1. Example: command &> [Link].

44. What is a while loop?


• Answer: A while loop executes code as long as the condition is true.
Example:
• while [ $i -lt 5 ]; do
echo $i
((i++))
done

45. What is a for loop in shell scripting?


• Answer: A for loop iterates over a list of items. Example:
• for i in 1 2 3; do
echo $i
done

46. How can you use command substitution in shell scripting?


• Answer: Use $(command) or backticks. Example: result=$(date) or
result=\date“.

47. What does #!/bin/bash mean?


• Answer: It’s the shebang line indicating that the script should be run
using the /bin/bash shell.

2
48. What is case used for in shell scripting?
• Answer: case is used for pattern matching and branching. Example:
• case $1 in
start) echo "Starting";;
stop) echo "Stopping";;
esac

49. What is cron and how is it used?


• Answer: cron is a job scheduler that runs scripts at specified times. Add
jobs using crontab -e.

50. How do you find the number of lines in a file?


• Answer: Use wc -l. Example: wc -l filename outputs the number of
lines in filename.

51. What does echo $0 print in a script?


• Answer: It prints the name of the current script.

52. How do you pass arguments to a shell script?


• Answer: Pass arguments when running the script like ./[Link] arg1
arg2. Access them using $1, $2, etc.

53. What does $? represent in a shell script?


• Answer: $? holds the exit status of the last executed command.

54. How do you use logical AND and OR in shell scripting?


• Answer: Use && for AND and || for OR. Example: command1 &&
command2.

55. What does shift do in a script?


• Answer: shift shifts positional parameters to the left, changing $2 to
$1, $3 to $2, and so on.

56. What is the use of eval in a shell script?


• Answer: eval runs a string as a shell command. Example: eval ls.

57. How do you create an array in shell scripting?


• Answer: Use arr=(value1 value2 value3). Access elements with
${arr[index]}.

3
58. What is source used for?
• Answer: source runs a script in the current shell without creating a new
shell process.

59. How do you check if a directory exists?


• Answer: Use -d. Example: if [ -d dirname ]; then echo
"Directory exists"; fi.

60. What is sed used for?


• Answer: sed is a stream editor used for text manipulation. Example:
sed 's/old/new/' file.

61. How do you replace text in a file using sed?


• Answer: sed -i 's/old/new/g' filename replaces all occurrences of
old with new.

62. What does the awk command do?


• Answer: awk processes and analyzes text files. Example: awk '{print
$1}' file prints the first column.

63. How do you debug a shell script?


• Answer: Run the script with bash -x [Link] for step-by-step debug-
ging.

64. What does trap do in shell scripting?


• Answer: trap catches signals and executes code when they occur. Exam-
ple: trap "echo 'Exiting'; exit" SIGINT.

65. What is the difference between > and >>?


• Answer: > overwrites a file, while >> appends to it.

66. What does set -e do in a shell script?


• Answer: It causes the script to exit immediately if any command returns
a non-zero status.

67. How do you write a multi-line comment?


• Answer: Use : ' for multi-line comments:

4
• : '
This is a multi-line
comment block
'

68. How do you create a temporary file in a shell script?


• Answer: Use mktemp. Example: tmpfile=$(mktemp).

69. What is printf and how is it different from echo?


• Answer: printf is more flexible and supports formatting. Example:
printf "Hello %s\n" "World".

70. What is the purpose of read -p?


• Answer: read -p allows you to take user input with a prompt. Example:
read -p "Enter your name: " name.

71. How do you check for a non-empty string in a shell script?


• Answer: Use [ -n "$var" ] to check if var is non-empty.

72. What does export do?


• Answer: export makes a variable available to child processes.

73. How do you perform integer arithmetic in a shell script?


• Answer: Use $((expression)). Example: result=$((3 + 2)).

74. What is bc used for in shell scripts?


• **Answer
**: bc is a command-line calculator for more complex arithmetic. Example:
echo "3.4 + 2.1" | bc.

75. How do you create a simple menu in a shell script?


• Answer:
• echo "1. Option 1"
echo "2. Option 2"
read choice
case $choice in
1) echo "Option 1 chosen";;
2) echo "Option 2 chosen";;
esac

5
76. What is the difference between single and double square brackets
in if?
• Answer: [[ ... ]] supports advanced pattern matching and is more
flexible than [ ... ].

77. How do you capture command output into a variable?


• Answer: Use $(command). Example: output=$(ls).

78. What is the difference between $* and $@?


• Answer: $* treats all arguments as a single word, $@ treats them as
separate words.

79. How do you run a command in the background?


• Answer: Use &. Example: sleep 10 &.

80. What is a subshell?


• Answer: A subshell is a separate instance of the shell that runs commands
in a new environment, created using ().

81. What does grep do in a shell script?


• Answer: grep searches for patterns in text. Example: grep "pattern"
file.

82. How do you find and replace text in multiple files?


• Answer: Use sed with find. Example: find . -type f -exec sed -i
's/old/new/g' {} +.

83. How do you create a simple progress bar in shell scripting?


• Answer:
• for i in {1..10}; do
echo -n "#"
sleep 1
done

84. What is xargs used for?


• Answer: xargs builds and executes command lines from standard input.
Example: echo "file1 file2" | xargs rm.

6
85. What does tail -f do?
• Answer: tail -f outputs appended data as a file grows, useful for
monitoring logs in real-time.

86. How do you handle errors in a shell script?


• Answer: Use set -e to stop the script on error, or check $? after
commands.

87. What does the cut command do?


• Answer: cut extracts sections from each line of input. Example: cut
-d',' -f2 file extracts the second field.

88. How do you create symbolic links in Linux?


• Answer: Use ln -s. Example: ln -s /path/to/original
/path/to/link.

89. What is ulimit in shell scripting?


• Answer: ulimit sets user limits for system resources. Example: ulimit
-n changes the max number of open files.

90. How do you compare strings in shell scripting?


• Answer: Use = or !=. Example: [ "$str1" = "$str2" ].

91. What does fg and bg do?


• Answer: fg brings a job to the foreground, bg runs a job in the back-
ground.

92. What is the use of nohup?


• Answer: nohup runs a command immune to hangups, continuing even
after logout. Example: nohup command &.

93. How do you set default values for variables?


• Answer: Use ${var:-default}. Example: echo ${name:-John}.

94. How do you create a numbered sequence in a loop?


• Answer: Use seq. Example:
• for i in $(seq 1 10); do
echo $i
done

7
95. How do you use conditional expressions in shell scripting?
• Answer: Use [ condition ] or [[ condition ]]. Example: if [ "$a"
-eq "$b" ]; then ... fi.

96. What does type command do?


• Answer: type shows how a command would be interpreted (builtin, alias,
path).

97. How do you combine multiple commands in one line?


• Answer: Use ;, &&, or ||. Example: command1 && command2.

98. What is the difference between == and = in shell?


• Answer: == is used in [[ ... ]] for string comparison; = can be used in
[ ... ].

99. How do you find the process ID (PID) of a running process?


• Answer: Use pgrep or ps. Example: pgrep process_name.

100. What does echo -n do?


• Answer: echo -n prints text without a trailing newline.

You might also like