DevOps

Shell Scripting & Bash

Variables, loops, conditionals, functions, pipes, redirections

20 interview questions·
Mid-Level
1

What is the correct syntax to declare a variable in Bash?

Answer

In Bash, a variable is declared without the dollar sign and without spaces around the equals sign. The variable name must start with a letter or underscore. To use the variable's value, prefix its name with the dollar sign ($). Spaces around the equals sign cause a syntax error because Bash interprets them as separate command arguments.

2

How to access the value of a variable named USERNAME in Bash?

Answer

To access a variable's value, prefix its name with the dollar sign ($). The syntax $USERNAME retrieves the value stored in the variable. The syntax ${USERNAME} can also be used, which offers more flexibility, especially for concatenation or using modifiers. Without the dollar sign, Bash interprets USERNAME as a literal string and not as a variable reference.

3

Which command allows exporting a variable to make it accessible to child processes?

Answer

The export command makes an environment variable available to all child processes launched from the current shell. Without export, a variable is local to the current shell and is not passed to subprocesses. This distinction is crucial when writing scripts that invoke other scripts or programs. It is possible to combine declaration and export in one line with export VAR=value.

4

How to capture the exit code of the last executed command?

5

What is the correct syntax for an if condition in Bash?

+17 interview questions

Master DevOps for your next interview

Access all questions, flashcards, technical tests, code review exercises and interview simulators.

Start for free