In a Bash script, a flag is a command-line option that controls the behavior of the script. Flags are often used to enable or disable certain features of the script, or to specify input or output files.
Flags are typically specified on the command line when running the script, and are usually preceded by a single dash (-
) or double dash (--
).
For example, consider the following Bash script that takes a flag -f
to specify an input file:
#!/bin/bash # Parse command-line arguments while getopts "f:" opt; do case $opt in f) input_file=$OPTARG ;; esac done # Do something with the input file echo "Input file: $input_file"Source.www:lautturi.com
To run this script and specify an input file, you would use the following command:
./myscript -f input.txt
This would set the value of the input_file
variable to input.txt
and the script would "do something" with it.
You can use multiple flags in a single command. For example:
./myscript -f input.txt -v
In this example, the -v
flag might be used to enable verbose output from the script.