parsing_arguments_in_bash_script

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.txt
  • Last modified: 20.01.2018 10:19
  • by Pascal Suter