parsing_arguments_in_bash_script

This is an old revision of the document!


Parsing arguments in bash script

you can access positional arguments through the $1 $2 etc. varaibles and $# contains the number of positional arguments, but what if you want to parse more advanced, non positional arguments? this simple snippet that i stole from here provides an elegant and quick solution:

interactive=
filename=~/sysinfo_page.html

while [ "$1" != "" ]; do
    case $1 in
        -f | --file )           shift
                                filename=$1
                                ;;
        -i | --interactive )    interactive=1
                                ;;
        -h | --help )           usage
                                exit
                                ;;
        * )                     usage
                                exit 1
    esac
    shift
done
  • parsing_arguments_in_bash_script.1512555256.txt.gz
  • Last modified: 06.12.2017 11:14
  • by Pascal Suter