Is there a way to feed non positional arguments to a shell script?
Meaning explicitly specify some kind of flag?
. myscript.sh value1 value2
. myscript.sh -val1=value1 -val2=value2
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can use
getopts, but I don’t like it because it’s complicated to use and it doesn’t support long option names (not the POSIX version anyway).I recommend against using environment variables. There’s just too much risk of name collision. For example, if your script reacts differently depending on the value of the
ARCHenvironment variable, and it executes another script that (unbeknownst to you) also reacts to theARCHenvironment variable, then you probably have a hard-to-find bug that only shows up occasionally.This is the pattern I use: