Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a local variable inside the function.
Bash Scripting
my_function() {
[1] my_var="Hello"
echo "$my_var"
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'global' instead of 'local' which is not valid in bash.
Using 'var' which is not a bash keyword.
Using 'declare' without the local keyword does not limit scope.
✗ Incorrect
The 'local' keyword declares a variable that is only visible inside the function.
2fill in blank
mediumComplete the code to print the local variable inside the function.
Bash Scripting
function greet() {
local message="Hi"
echo [1]
}
greet Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Printing the variable name without $ which outputs the name, not the value.
Using quotes around the variable name which prints the literal string.
✗ Incorrect
To print the value of a variable in bash, prefix it with a dollar sign ($).
3fill in blank
hardFix the error in the function by declaring the variable local.
Bash Scripting
count=5 increment() { [1] count=$((count + 1)) echo $count } increment echo $count
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'global' which is not a bash keyword.
Using 'export' which affects environment variables, not local scope.
✗ Incorrect
Declaring 'count' as local inside the function prevents changing the global variable.
4fill in blank
hardFill both blanks to declare a local variable and assign it a value.
Bash Scripting
my_func() {
[1] [2]="World"
echo "$greeting"
}
my_func Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'var' which is not a bash keyword.
Using a variable name different from the one echoed.
✗ Incorrect
Use 'local' to declare the variable and 'greeting' as the variable name.
5fill in blank
hardFill all three blanks to declare a local variable, assign it, and print it.
Bash Scripting
function say() {
[1] [2]="Hello"
echo [3]
}
say Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Not using local keyword.
Printing variable name without $.
Using different variable names inconsistently.
✗ Incorrect
Declare 'greet' as local, assign it, and print its value with $.