How to get arguments with flags in Bash

后端 未结 11 2097
余生分开走
余生分开走 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:21

    #!/bin/bash
    
    if getopts "n:" arg; then
      echo "Welcome $OPTARG"
    fi
    

    Save it as sample.sh and try running

    sh sample.sh -n John
    

    in your terminal.

提交回复
热议问题