0
0
Bash Scriptingscripting~10 mins

Local variables (local keyword) in Bash Scripting - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
Alocal
Bglobal
Cdeclare
Dvar
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.
2fill in blank
medium

Complete 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'
Amessage
B$message
C"message"
D'message'
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.
3fill in blank
hard

Fix 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'
Aexport
Bglobal
Clocal
Ddeclare
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'global' which is not a bash keyword.
Using 'export' which affects environment variables, not local scope.
4fill in blank
hard

Fill 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'
Alocal
Bgreeting
Cmessage
Dvar
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'var' which is not a bash keyword.
Using a variable name different from the one echoed.
5fill in blank
hard

Fill 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'
Alocal
Bgreet
C$greet
Dmessage
Attempts:
3 left
💡 Hint
Common Mistakes
Not using local keyword.
Printing variable name without $.
Using different variable names inconsistently.