To pass command-line arguments to a Bash script, you can follow these steps:
Create a Bash script file with a .sh extension, for example, script.sh
.
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.
Use these variables within the script to process or manipulate the command-line arguments as needed.
Save and close the script file.
Open a terminal or command prompt and navigate to the directory where the script file is located.
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.