Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Is there any easy way to pass (receive) named parameters to a shell script?

For example,

my_script -p_out '/some/path' -arg_1 '5'

And inside receive them as:

# I believe this notation does not work, but is there anything close to it?

printf "Argument p_out is %s" "$p_out"
printf "Argument arg_1 is %s" "$arg_1"

Is this possible in Bash or Zsh?

share|improve this question
have a look at docopt – it helps with named parameters and does input validation, too – Beat May 14 '14 at 21:55

5 Answers 5

up vote 10 down vote accepted

The probably closest syntax to that is:

p_out='/some/path' arg_1='5' my_script
share|improve this answer
Related to this, if the -k option is set in the calling shell, then my_script p_out='/some/path' arg_1='5' has the same effect. (All arguments in the form of an assignment are added to the environment, not just those assignments preceding the command.) – chepner May 14 '14 at 22:55
I used to love this syntax, but it has a BIG caveat: after the command/function execution, those variables will still be defined in the current scope ! E.g.: x=42 echo $x; echo $x Which means in the next execution of my_script, if p_out is omitted, it will stick to the value passed the last time !! ('/some/path') – Lucas Cimon Sep 1 at 9:13

If you don't mind being limited to single-letter argument names i.e. my_script -p '/some/path' -a5, then in bash you could use the built-in getopts, e.g.


while getopts ":a:p:" opt; do
  case $opt in
    a) p_out="$OPTARG"
    p) arg_1="$OPTARG"
    \?) echo "Invalid option -$OPTARG" >&2

printf "Argument p_out is %s\n" "$p_out"
printf "Argument arg_1 is %s\n" "$arg_1"

Then you can do

$ ./my_script -p '/some/path' -a5
Argument p_out is 5
Argument arg_1 is /some/path

There is a helpful Small getopts tutorial or you can type help getopts at the shell prompt.

share|improve this answer
This should be the accepted answer – Kaushik Ghose May 21 at 13:38

With zsh, you'd use zparseopts:

#! /bin/zsh -
zmodload zsh/zutil
zparseopts -A ARGUMENTS -p_out: -arg_1:


printf 'Argument p_out is "%s"\n' "$p_out"
printf 'Argument arg_1 is "%s"\n' "$arg_1"

But you'd call the script with myscript --p_out foo.

Note that zparseopts doesn't support abbreviating long options or the --p_out=foo syntax like GNU getopt(3) does.

share|improve this answer

I stole this from, but you could do something like this:

while [ $# -gt 0 ]; do
  case "$1" in
      printf "***************************\n"
      printf "* Error: Invalid argument.*\n"
      printf "***************************\n"
      exit 1

The only caveat is that you have to use the syntax my_script --p_out=/some/path --arg_1=5.

share|improve this answer
mitsos@redhat24$ my_script "a=1;b=mitsos;c=karamitsos"
eval "$1"

you've just injected command line parameters inside script scope !!

share|improve this answer
This doesn't work with the syntax the OP specified; they want -a 1 -b mitsos -c karamitsos – Michael Mrozek Feb 18 at 14:48

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.