0
0
Bash Scriptingscripting~5 mins

Function arguments ($1, $2 inside function) in Bash Scripting - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What do $1 and $2 represent inside a bash function?

$1 and $2 are the first and second arguments passed to the function when it is called.

Click to reveal answer
beginner
How do you access the third argument inside a bash function?

You use $3 to access the third argument passed to the function.

Click to reveal answer
beginner
True or False: $1 inside a bash function refers to the first argument passed to the script, not the function.

False. Inside a function, $1 refers to the first argument passed to that function, not the script.

Click to reveal answer
beginner
How can you pass arguments to a bash function?

You call the function followed by the arguments separated by spaces, like myfunc arg1 arg2. Inside the function, $1 will be arg1, $2 will be arg2.

Click to reveal answer
beginner
What happens if you call a bash function without arguments but try to use $1 inside it?

$1 will be empty or unset because no argument was passed to the function.

Click to reveal answer
Inside a bash function, what does $2 represent?
AThe second environment variable
BThe second argument passed to the script
CThe second argument passed to the function
DThe second line of the script
How do you pass arguments to a bash function named greet?
Agreet arg1 arg2
Bgreet(arg1, arg2)
Cgreet[arg1, arg2]
Dgreet: arg1 arg2
What will $1 be inside a function if you call it without any arguments?
AAn error message
BThe first argument of the script
CThe function name
DEmpty or unset
Which symbol is used to access the first argument inside a bash function?
A#1
B$1
C&1
D*1
If a function is called as func apple banana, what is $2 inside func?
Abanana
Bapple
Cfunc
Dempty
Explain how to use $1 and $2 inside a bash function with an example.
Think about how you pass and use arguments in a function.
You got /4 concepts.
    What happens if you try to use $1 inside a bash function but you did not pass any arguments when calling it?
    Consider what $1 means when no argument is given.
    You got /4 concepts.