Question: What different types of loops are supported in Bash scripts?
|
Answer: Following three types of loops are supported in Bash:
• The ‘for’ loop: for i in {1..10}; do echo $i; done
• The ‘while’ loop: i=1; while [ $i -le 10 ]; do echo $i; let "i++"; done
• The ‘until’ loop: i=1; until [ $i -gt 10 ]; do echo $i; let "i++"; done
|
Question: How can one exit a loop?
|
Answer: One would need to use a break statement to exit the loop.
Generally before using break statement if statement is used
to decide when a loop should be exited.
|
Question: How to confirm that you are using a bash shell?
|
Answer: Echoing the $SHELL variable one can verify the same:
echo $SHELL # prints /bin/bash
|
Question: What does it mean by a Shell variable?
|
Answer: Variables in any shell script store data for use by the program:
$myvariable = "Hi"
echo $myvariable
|
Question: Can you mention what are the different types of variables that are used?
|
Answer: Usually two types of variables are used in shells:
• System-defined (environment) variables: This are built-in variables within Linux kernel, like: SHELL.
• User-defined variables: variables created by a user to store, read or manipulate data, like: $i = 5
|
Question: How can you compare strings within a shell script?
|
Answer: To compare text strings, one can use ‘test’ command.
This command compares text strings by comparing each character of each string.
|
Question: Is there any 3rd party tool to analyze Bash scripts?
|
Answer: There is a static analysis tool named ShellCheck what can be used to catch syntax errors, any unused variable and also the common mistakes within the shell script.
|
Question: Why ‘shebang’ is important in Bash scripts?
|
Answer: A ‘shebang’ in Bash script is the starting line with “#!” followed by a path.
It informs the system which shell interpreter to use for executing the script, like: ‘#!/bin/bash’ .
|