Debug School

Akanksha
Akanksha

Posted on

Top 30 Bash Scripting Interview Questions with Answers

1. What does the command echo $SHELL do?

A. Prints the current shell
B. Prints the home directory
C. Prints the system environment
D. Prints the user's home directory
Answer: A. Prints the current shell

2. Which symbol is used for commenting in a Bash script?

A. //
B. #
C. --
D. %
Answer: B. #

3. In Bash scripting, what does chmod +x script.sh do?

A. Adds execute permission to the script.sh file
B. Removes execute permission from the script.sh file
C. Renames the script.sh file
D. Changes the ownership of the script.sh file
Answer: A. Adds execute permission to the script.sh file

4. What does $0 represent in a Bash script?

A. The current process ID
B. The first command-line argument
C. The script's name
D. The number of arguments passed
Answer: C. The script's name

5. To assign a value to a variable in Bash, which syntax is correct?

A. variable=value
B. variable = value
C. value = variable
D. variable := value
Answer: A. variable=value

6. What does the command ls | grep 'pattern' do?

A. Lists all files in the directory
B. Lists files containing 'pattern' in their names
C. Lists directories only
D. Lists hidden files in the directory
Answer: B. Lists files containing 'pattern' in their names

7. Which command is used to remove a directory in Bash?

A. delete
B. rmdir
C. rm
D. del
Answer: B. rmdir

8. In Bash, how do you check if a file exists?

A. check file
B. test -f
C. file_exists
D. exists
Answer: B. test -f

9. Which command is used to read user input in Bash scripting?

A. input
B. read
C. get
D. userinput
Answer: B. read

10. What does the if statement in Bash evaluate?

A. Integer values
B. Strings
C. Boolean expressions
D. Functions
Answer: C. Boolean expressions

11. To run a Bash script in the background, which symbol is used?

A. &
B. *
C. #
D. $
Answer: A. &

12. Which command is used to display the last 10 lines of a file in Bash?

A. last
B. cat -n
C. tail
D. head
Answer: C. tail

13. Which command is used to redirect standard error to a file in Bash?

A. 2>
B. 1>
C. &>
D. |
Answer: A. 2>

14. What does the case statement in Bash allow you to do?

A. Define functions
B. Perform pattern-based branching
C. Assign variables
D. Define arrays
Answer: B. Perform pattern-based branching

15. Which command is used to list all environment variables in Bash?

A. env
B. printenv
C. variables
D. listenv
Answer: B. printenv

16. Which symbol is used to concatenate strings in Bash?

A. +
B. .
C. ,
D. :
Answer: B. .

17. To find and replace text within a file in Bash, which command is commonly used?

A. replace
B. findreplace
C. sed
D. awk
Answer: C. sed

18. In Bash, which command is used to pause the script execution for a specific duration?

A. wait
B. sleep
C. pause
D. delay
Answer: B. sleep

19. What does the expr command do in Bash?

A. Executes an external program
B. Evaluates arithmetic expressions
C. Lists files in a directory
D. Prints environment variables
Answer: B. Evaluates arithmetic expressions

20. Which command is used to create a new directory in Bash?

A. create
B. mkdir
C. touch
D. newdir
Answer: B. mkdir

21. Which command is used to sort the lines of a file in Bash?

A. sort
B. order
C. arrange
D. organize
Answer: A. sort

22. What does the trap command do in Bash?

A. Sets a trap for bugs
B. Handles signals in the script
C. Defines an exception handler
D. Prints a message
Answer: B. Handles signals in the script

23. To delete a file in Bash, which command is used?

A. delete
B. rm
C. erase
D. del
Answer: B. rm

24. Which command is used to compare two files in Bash?

A. diff
B. compare
C. cmp
D. contrast
Answer: A. diff

25. In Bash, how do you declare an array?

A. array = (value1 value2 value3)
B. array := (value1 value2 value3)
C. array (value1 value2 value3)
D. array=(value1 value2 value3)
Answer: D. array=(value1 value2 value3)

26. Which command is used to display the first 10 lines of a file in Bash?

A. first
B. cat -n
C. tail
D. head
Answer: D. head

27. In Bash, how do you check if a directory exists?

A. checkdir
B. test -d
C. dir_exists
D. existsdir
Answer: B. test -d

28. Which command is used to calculate the length of a string in Bash?

A. length
B. strlen
C. strlength
D. charcount
Answer: B. strlen

29. In Bash scripting, what does $# represent?

A. The process ID
B. The first command-line argument
C. The number of arguments passed
D. The script's name
Answer: C. The number of arguments passed

30. Which command is used to display the contents of a file in reverse order in Bash?

A. rev
B. reverse
C. invert
D. backwards
Answer: A. rev

Top comments (0)