**
In the realm of bash scripting, what does it mean to have "$1" or "$2" or even "$99"? Simply put, they are special variables that fetch arguments passed to your script. When writing bash scripts, you can access these variables to manipulate or display the command-line arguments. Let's delve into this in more detail.

**
Understanding Arguments in Bash Scripts

Every bash script, when invoked, can accept specific inputs, known as arguments. These arguments are space-separated strings that follow the script name in the command line. The first argument is referred to as "$1", the second as "$2", and so on. The special variable "$0" represents the name of the script itself.
To visualize this, let's say you have a script named "greet.sh" with the following lines:

#!/bin/bash echo "Hello, $1!"
If you run this script with an argument, like "bash greet.sh World", it will output: "Hello, World!". Here, "$1" is replaced by "World".
Accessing Arguments by Their Position

The position of an argument in "$1", "$2", etc., starts from 1, not 0. This might seem odd, but it's because the count starts from the first argument, not the script name. So, the script name itself is "$0", followed by "$1", "$2", and so on.
You can access any number of arguments provided, up to("$9"), but for more than nine arguments, you'll need to use special notation like "${10}", "${11}", etc. But remember, these variables are read-only. You can't change their values within the script.
Accessing All Arguments at Once

If you want to access all arguments at once, you can use the special array-like variable "*". The statements "$*" and "${*" both expand to the entire list of arguments. The difference lies in how they handle arguments with spaces. "$*" joins all arguments into a single string with spaces, while "${*" preserves the individual arguments even if they have spaces.
For example, running "bash test.sh one two" with the script:
#!/bin/bash echo "I received: $*"
Will output: "I received: one two". But using "${*" instead would output: "I received: one two" as a single string.
Now that you understand how to access arguments in bash scripts, you can start incorporating this functionality into your scripts to make them more dynamic and user-friendly.

Practical Applications of Arguments in Bash Scripts
Arguments are powerful tools for creating versatile bash scripts. They allow you to pass specific data at runtime, making your scripts adaptable to various scenarios without needing to hardcode values.


![30+ Effective Bash Script Examples You Need to Know [Updated]](https://i.pinimg.com/originals/de/16/c5/de16c5bd1d0f22e4bbcb3ad983cb062d.webp)



Greeting Script
A simple greeting script, as shown earlier, can be enhanced to greet multiple people by accepting arguments. Here's how:
#!/bin/bash for person in "$@"; do echo "Hello, $person!" done
Running "bash greet.sh Alice Bob Charlie" will now greet all three.
File Processing Script
Advanced scripts can utilize arguments for file processing. For instance, a script that takes a filename and a delimiter, then displays each line along with its number and split fields:
#!/bin/bash
if [[ -n "$1" && -f "$1" ]]; then
delimiter="$2"
while IFS="$delimiter" read -r line; do
echo "Line $line_number: $line"
done <"$1"
line_number=$((line_number+1))
fi
This script uses "$1" for the filename and "$2" for the delimiter. It reads the file, splits the lines based on the delimiter, and displays each line with its number and fields.
So, next time you're writing a bash script and you want to make it interactive or adaptable, don't forget to implement arguments using "$1", "$2", etc. They are a simple yet powerful feature for enhancing your scripts.
The journey of mastering bash scripting never ends. With each script, you gain more understanding and experience. So, keep exploring, practicing, and never shy away from challenging yourself with new projects. Happy coding!