How to pass command-line arguments to a Bash script?

To pass command-line arguments to a Bash script, you can follow these steps:

  1. Create a Bash script file with a .sh extension, for example, script.sh.

  2. Declare function arguments inside the script by using special variables like $1, $2, $3, etc., where $1 represents the first argument, $2 represents the second argument, and so on.

  3. Use these variables within the script to process or manipulate the command-line arguments as needed.

  4. Save and close the script file.

  5. Open a terminal or command prompt and navigate to the directory where the script file is located.

  6. Use the bash command followed by the script file name and the desired arguments to execute the script. For example:

$ bash script.sh argument1 argument2

The script will receive the provided arguments, and you can access them within the script using the $1, $2, etc., variables.

Note: The special variable $0 represents the script name itself, similar to argv[0] in other programming languages.