How to get arguments with flags in Bash

后端 未结 11 2141
余生分开走
余生分开走 2020-12-22 15:56

I know that I can easily get positioned parameters like this in bash:

$0 or $1

I want to be able to use flag options like this to s

11条回答
  •  北荒
    北荒 (楼主)
    2020-12-22 16:18

    If you're familiar with Python argparse, and don't mind calling python to parse bash arguments, there is a piece of code I found really helpful and super easy to use called argparse-bash https://github.com/nhoffman/argparse-bash

    Example take from their example.sh script:

    #!/bin/bash
    
    source $(dirname $0)/argparse.bash || exit 1
    argparse "$@" <

提交回复
热议问题