Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to print the first argument passed to the function.
Bash Scripting
my_func() {
echo [1]
}
my_func Hello Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using $0 instead of $1 to get the first argument.
Using $2 when only one argument is passed.
✗ Incorrect
Inside a bash function, $1 refers to the first argument passed to that function.
2fill in blank
mediumComplete the code to print the second argument passed to the function.
Bash Scripting
print_second() {
echo [1]
}
print_second apple banana Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using $1 instead of $2 to get the second argument.
Using $# which gives the number of arguments, not the argument itself.
✗ Incorrect
Inside the function, $2 refers to the second argument passed to it.
3fill in blank
hardFix the error in the function to correctly print the first and second arguments.
Bash Scripting
show_args() {
echo "First: [1]"
echo "Second: $2"
}
show_args one two Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using $2 for the first argument.
Using $0 which is the script name, not function argument.
✗ Incorrect
The first argument inside the function is accessed with $1, not $2 or $0.
4fill in blank
hardFill both blanks to print the first and second arguments inside the function.
Bash Scripting
args() {
echo "Arg1: [1]"
echo "Arg2: [2]"
}
args red blue Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping $1 and $2.
Using $3 or $# instead of $1 or $2.
✗ Incorrect
Use $1 for the first argument and $2 for the second argument inside the function.
5fill in blank
hardFill all three blanks to create a function that prints all three arguments passed to it.
Bash Scripting
print_all() {
echo "First: [1]"
echo "Second: [2]"
echo "Third: [3]"
}
print_all one two three Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using $# which gives the count, not the argument value.
Mixing up the order of $1, $2, and $3.
✗ Incorrect
Use $1, $2, and $3 to access the first, second, and third arguments inside the function.