0
0
Bash Scriptingscripting~20 mins

Special variables ($0, $1, $#, $@, $?, $) in Bash Scripting - Mini Project: Build & Apply

Choose your learning style9 modes available
Using Special Variables in Bash Scripts
📖 Scenario: You are writing a simple bash script to understand how special variables work. These variables help you get information about the script name, arguments passed, number of arguments, process ID, and the exit status of commands.
🎯 Goal: Build a bash script that uses the special variables $0, $1, $#, $@, $?, and $$ to display useful information about the script and its arguments.
📋 What You'll Learn
Create a variable to hold the first argument using $1
Create a variable to hold the number of arguments using $#
Create a variable to hold all arguments using $@
Use $0 to get the script name
Use $$ to get the current process ID
Use $? to get the exit status of the last command
Print all these values clearly
💡 Why This Matters
🌍 Real World
Scripts often need to know their own name, what arguments were passed, and how commands executed to behave correctly.
💼 Career
Understanding special variables is essential for writing robust bash scripts used in automation, system administration, and DevOps tasks.
Progress0 / 4 steps
1
Create variables for script name and first argument
Create a variable called script_name and set it to $0. Then create a variable called first_arg and set it to $1.
Bash Scripting
Need a hint?

Use script_name=$0 to get the script name and first_arg=$1 to get the first argument.

2
Add variables for argument count and all arguments
Add a variable called arg_count and set it to $#. Add another variable called all_args and set it to $@.
Bash Scripting
Need a hint?

Use arg_count=$# for number of arguments and all_args="$@" for all arguments.

3
Add variables for last command status and process ID
Add a variable called last_status and set it to $?. Add a variable called process_id and set it to $$.
Bash Scripting
Need a hint?

Use last_status=$? for last command exit status and process_id=$$ for current process ID.

4
Print all special variable values
Print the values of script_name, first_arg, arg_count, all_args, last_status, and process_id using echo. Each value should be on its own line with a clear label.
Bash Scripting
Need a hint?

Use echo to print each variable with a label. For example, echo "Script name: $script_name".