0
0
Bash Scriptingscripting~10 mins

Function arguments ($1, $2 inside function) 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 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'
A$1
B$2
C$0
D$#
Attempts:
3 left
💡 Hint
Common Mistakes
Using $0 instead of $1 to get the first argument.
Using $2 when only one argument is passed.
2fill in blank
medium

Complete 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'
A$#
B$2
C$3
D$1
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.
3fill in blank
hard

Fix 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'
A$1
B$2
C$0
D$#
Attempts:
3 left
💡 Hint
Common Mistakes
Using $2 for the first argument.
Using $0 which is the script name, not function argument.
4fill in blank
hard

Fill 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'
A$1
B$2
C$3
D$#
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping $1 and $2.
Using $3 or $# instead of $1 or $2.
5fill in blank
hard

Fill 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'
A$1
B$2
C$3
D$#
Attempts:
3 left
💡 Hint
Common Mistakes
Using $# which gives the count, not the argument value.
Mixing up the order of $1, $2, and $3.