Passing shell script arguments to a subprocess

So I want to create a shell script that ultimately exec's a command, after doing something like, say, setting an environment variable first:

export MY_VAR=MY_VAL
exec my_command $*

(The point of using `exec my_command` rather than plain `my_command` is to not leave a /bin/sh process waiting for my_command that shows up in pstree and makes my_command not see its "real" parent process and so on.)

Simple enough, except it doesn't work. If you run the script with `script "a b" c`, my_command's arguments will be a b c (that's three arguments instead of the original two).

(Update – as pointed out in the comments, "$@" instead of $* works fine, and is perfectly sufficient for my example where all the script does is setting an env var. "$@" isn't enough if you need to fiddle with the arguments – if you need to do that, read on, otherwise just use "$@".)

A common workaround seems to be, you iterate over the arguments and you quote them and then eval:

export MY_VAR=MY_VAL
for arg in "$@";
  args="$args '$arg'"
eval exec my_command $args

Not so simple, but works better: "a b" c will indeed be passed to my_command as "a b" c.

However, it doesn't work if the arguments contain single quotes. If you pass "'a'" (that's double quote, single quote, the character a, single quote, double quote), my_command will get plain a. If you pass "'a b'" (double, single, a, space, b, single, double), my_command will get two arguments, a b, instead of one, 'a b'.

What to do? One potential workaround is escaping quotes: replacing ' with ', etc. Perhaps someone with sufficiently thorough understanding of the Unix shell could pull it off; personally, I wouldn't trust myself to take care of all the special cases, or even to fully enumerate them.

So instead, what works for me (or so I hope) is, instead of creating a string of values by concatenating arguments, I make a string of references to the arguments using the variables $1, $2, etc. How many of those are needed – is the last one $3 or $7? Ah, we can use $# to figure that out:

export MY_VAR=MY_VAL
while [ $nargs -gt 0 ]
  args=""$$nargs" $args"
  nargs=`expr $nargs - 1`
eval exec my_command $args

This handsome code generates, given three arguments, the string "$1" "$2" "$3", and then evals it to get the three argument values, which apparently can't cause quoting complications that are dependent on the actual argument values. With five arguments, the string "$1" "$2" "$3" "$4" "$5" is generated, and so on. (You'd use more code if you wanted to mangle some of the arguments, which is the sole reason to do this sort of things as opposed to using "$@".)

If you're good at Unix and you know a less ""$$ugly" $way" to do this, and/or a more correct one, do tell.

(Why am I writing shell scripts in the first place, you ask? There are reasons for this too, and reasons for the reasons; there always are.)

Update 2: according to a comment, $1-$9 work in sh, but $10 and up do not; they do work in bash, which is what you actually get when you ask for /bin/sh on some systems but not others.

I really ought to try harder to stay away from shell scripting.  I mean, I know I shouldn't, but I keep coming back. I'm like those tribesmen around the world who can't resist the urge to drink alcohol and having genes making alcohol particularly addictive and bad for their health. I clearly don't have the genetic makeup that would make *sh reasonably harmless for me.


#1 Barry Kelly on 03.08.12 at 8:26 am

Hm. I use:

exec my_command "$@"

which quotes it properly for you. For more complicated scenarios, I use bash arrays and "${args[@]}" – it's not usually worthwhile avoiding bash to get this all working portably.

#2 Daniel on 03.08.12 at 8:57 am

See `man bash` in the section "Special Parameters". You want @, not *, in the individually quoted form, like Barry mentioned.

#3 Nathan on 03.08.12 at 9:18 am

When performing basic arithmetic, at least in bash, you can do: nargs=$((nargs-1)) which should be faster than expr.

#4 Yossi Kreinin on 03.08.12 at 10:16 am

regarding the (()) syntax: yeah, but not in sh; Ubuntu's sh is a real sh and not a bash, for some reason, which breaks loads of scripts used to it being bash…

regarding "$@": it works if you don't need to fiddle with any of the arguments, which my example doesn't do (the SO example I linked to does.)

#5 Yossi Kreinin on 03.08.12 at 10:24 am

updated the text to mention "$@" – thanks!

#6 Lance on 03.08.12 at 12:56 pm

I routinely do "sudo dpkg-reconfigure dash" on Ubuntu installations to revert to bash, too much script breakage with the default "dash" shell.

#7 Petr Viktorin on 03.08.12 at 1:09 pm

And that is why shell sucks for anything over one line. It has lots of nifty shortcuts that save your time, but they tend to get in the way when you want robust scripts.
For one-liners, though, it's perfect.

Your ending comment should come a little earlier – the general solution for this should be to use a language where the arguments are in a proper $container of strings. Only when that's not possible, invoke black quote magic.

#8 Anton Kovalenko on 03.08.12 at 2:28 pm

I'd recommend relying on splicing and unsplicing (the latter may be emulated, see below) as black boxes instead of tampering with single or double quotes.

This one is pure sh (I hope it won't be messed up when posted):


store() { r="$@"; }

for arg in "$@" ; do
echo "Fiddling with $arg"
store $r "$arg"

echo "Args after fiddling: $r"

There once was a bug in an ancient sh, making "$@" expand incorrectly into "" (one empty argument). That's why you can meet ${1+"$@"} in some scripts, used instead of "$@", but I don't think you can stumble upon a shell where it's still necessary.

#9 alenvers on 03.09.12 at 9:37 am

One liner

args=`seq -s' ' -f '"$%1.0f"' 1 $#`

#10 saurabh on 03.09.12 at 10:02 am

Your workaround breaks down anyway for non-bash sh if you have more than 9 arguments.

#11 Leandro Lucarella on 03.09.12 at 12:40 pm

About $(()), yes, you can do it in sh too, but you have to use the regular parameter expansion syntax inside it: $(($nargs-1)). And is POSIX, in case you wonder…

#12 Decklin Foster on 03.09.12 at 6:57 pm

In some simpler cases you can use shift and set to modify the positional parameters. For example, to munge only the first one:

first="$1"; shift
upcased="$(echo "$first" | tr a-z A-Z)"
set — "$upcased" "$@"
exec my_command "$@"

Unfortunately I don't think (off the top of my head) there's a way to generalize this to unknown numbers of parameters.

#13 Yossi Kreinin on 03.10.12 at 7:18 am

OK, so you've all convinced me that my knowledge of the shell is insufficient for most practical purposes, and that I'd probably rather keep it that way than delude myself into believing that I'd ever improve… I'll try harder to use a language giving me an argument array next time.

And, as to there being no $i beyond $9 – thanks, I'll mention that…

#14 Kartik Agaram on 03.11.12 at 1:50 am

I'm surprised nobody's mentioned this, so I'll be that guy: use zsh.

#!/usr/bin/env zsh

#15 AllanLane5 on 03.13.12 at 9:23 am

Not to be snarky, but isn't this why Perl was invented? Portable, robust, and much more consistent than sh.

#16 robohack on 03.14.12 at 10:53 am

Here's a sample script I keep in my example sources for handy use.

It shows how to squirrel away "$@" for safe keeping and re-use.

It could work with V7 sh, I think, by using expr instead of shell arithmetic to increment the counter and of course by doing it inline instead of defining a function (and also of course losing the "local" declaration). One could probably do without expr too and just use $# and shift to decrement it, but that would lead to the confusing result in that the list_* variables would be numbered backwards.

A bit of trivia: with V7 sh, and probably most of its earlier derivatives, one would have to use ${1+"$@"} instead of just "$@" in order to avoid passing an empty parameter when none were originally supplied. See:

(AllanLane5: that was a joke right? You don't seriously think Perl is more consistent than sh, do you? LOL! Robust? ROTFL! maybe a bit more portable, but not since POSIX in, what, )

#! /bin/sh
# save_args — keep a list of quoted strings for later use via "$@"
# Note: creates a variable for each value, and uses these in another
# variable which can then be expanded with "eval" to re-reate the
# original parameters, preserving words containing whitespace
# See also for shell_quote() which can do the same, but
# produces its result on its stdout, and which does this simply using
# the normal single-quote characters.
# HOWEVER, shell_quote() requires printf(1) and sed(1), while this
# variant does it all internally to the shell.
local a c n
c=0; n=$1; shift

for a in "$@"; do
eval $n="$$n \"\$${n}_$c\""
eval ${n}_$c="$a"
# XXX Need to write free_args() to unset all the ${n}_$c variables!!!
# use:
# save_args list -foo bar "arg with spaces" "don't panic" '"Help!"'
# or:
# save_args list *.pdf # filenames with whitespace
# later:
# eval "gv ${list}"
# or, being a bit silly:
# . ./
# eval set — ${list}
# for str in "$@"; do
# q="$(shell_quote "$str")"
# list2="${list2}${list2:+ }${q}"
# done

#17 Aristotle Pagaltzis on 03.23.12 at 7:06 pm

Sure /bin/sh is a real sh – but does that also mean /bin/bash is absent? Because if you are worried only about Ubuntu, then the answer is No. And if the answer is No then you can just put /bin/bash in your shebang. (Just because Ubuntu makes you actually say that you want bash and not just sh when you do want bash and not just sh does not mean you cannot use bash at all.)

Then you can copy the args to an array variable using

local ARGS=( "$@" )

whose elements you can then molest to your heart’s desire. Once done, you run your program with

exec foo "${ARGS[@]}"

Very simple.

#18 Aristotle Pagaltzis on 03.23.12 at 7:09 pm

Kartik Agaram:

I believe the suggestion to use zsh falls under Yossi’s “use another language” clause. :-)

#19 Yossi Kreinin on 03.24.12 at 12:21 am

You know, I really should stay away from the shell. My mental faculties responsible for things like ("$@") and "${ARGS[@]}" are underdeveloped, so I'm just inflicting pain upon myself.

#20 Rakesh Sharma on 05.20.13 at 8:22 am

I'd rewrite the last line as:

exec my_command ${1+"$@"};

#21 nfomon on 12.20.13 at 7:32 am

SUSv3: "The parameter name or symbol can be enclosed in braces, which are optional except for positional parameters with more than one digit or when parameter is followed by a character that could be interpreted as part of the name."

That is, $10 is interpreted as $1 with a 0 afterwards. For positional parameters after $9, you should refer to them as ${10} etc.

Some shells (e.g. dash) fail this conformance and actually allow $10 to mean ${10}.

I'm working on a new non-POSIX command shell that cannot have this category of stupidity. Check it out: though note that this is EXTREMELY early, i.e. I haven't even really launched yet, and any feedback is appreciated…..

#22 Bilal on 10.28.14 at 6:39 pm

You save my day!

#23 ahervert on 05.07.15 at 2:37 am

nfomon that is the solution

#24 Eduardo Bustamante on 05.08.15 at 7:08 pm

This is safer and more straightforward than using eval:

dualbus@yaqui ~ % ./ foo bar baz
foo barbar bazbaz
dualbus@yaqui ~ % cat

export ENV_VAR

for arg do
case $arg in
*[abc]*) arg=$arg$arg;;

set "$@" "$arg"

shift "$nargs"

echo "$@"

# In case this doesn't show correctly here:

#25 dzan on 01.10.16 at 12:35 pm

Previous samles with 'set' work fine but marke sure to use

set — "$@" "$arg"

Otherwise if the first argument is a valid one for set it will use it, e.g. -x.

#26 Yossi Kreinin on 01.10.16 at 10:08 pm

Thanks! Yeah, I've seen — passed to every other command in git's shell source code… "Do one job and do it well…"

#27 devops training in hyderabad on 07.26.17 at 12:00 pm

Nice Article. In short description good explanation about the DevOps. Thanks For sharing the informative news.

#28 calfre2020 on 07.25.18 at 12:22 pm

very useful and knowledgeable information I simply stumbled upon your weblog

#29 anirudh on 03.29.19 at 4:03 pm

Thanks for the great article this is very useful info thanks for the wonderful post.

#30 cialis on 04.19.19 at 1:34 pm

Hello there! I simply want to give you a big thumbs up for the excellent information you
have got here on this post. I'll be returning to your blog
for more soon.

#31 free gg hack on 05.15.19 at 11:43 pm

I conceive this web site holds some real superb information for everyone : D.

#32 vn hax on 05.16.19 at 1:55 pm

Just wanna input on few general things, The website layout is perfect, the articles is very superb : D.

#33 aimbot fortnite on 05.16.19 at 5:49 pm

Great, google took me stright here. thanks btw for this. Cheers!

#34 nonsensediamond on 05.17.19 at 8:06 am

Great, yahoo took me stright here. thanks btw for this. Cheers!

#35 Mittie Kolstad on 05.17.19 at 11:02 am

5/17/2019 In my opinion, does a excellent job of covering subject matter of this type. Even if often deliberately polemic, the posts are in the main thoughtful and stimulating.

#36 fallout 76 cheats on 05.17.19 at 11:30 am

Yeah bookmaking this wasn’t a risky decision outstanding post! .

#37 red dead redemption 2 digital key resale on 05.17.19 at 4:36 pm

Ni hao, i really think i will be back to your site

#38 redline v3.0 on 05.17.19 at 7:43 pm

I have interest in this, thanks.

#39 badoo superpowers free on 05.18.19 at 9:09 am

This is good. Cheers!

#40 forza horizon 4 license key on 05.18.19 at 4:01 pm

Appreciate it for this howling post, I am glad I observed this internet site on yahoo.

#41 mining simulator codes 2019 on 05.19.19 at 8:04 am

I am not rattling great with English but I get hold this really easygoing to read .

#42 smutstone on 05.20.19 at 12:44 pm

Enjoyed reading through this, very good stuff, thankyou .

#43 redline v3.0 on 05.21.19 at 8:15 am

Intresting, will come back here later too.

#44 free fire hack version unlimited diamond on 05.21.19 at 5:37 pm

I am not rattling great with English but I get hold this really easygoing to read .

#45 nonsense diamond on 05.22.19 at 7:27 pm

I truly enjoy looking through on this web site , it holds superb content .

#46 krunker hacks on 05.23.19 at 7:47 am

Respect to website author , some wonderful entropy.

#47 bitcoin adder v.1.3.00 free download on 05.23.19 at 11:26 am

Respect to website author , some wonderful entropy.

#48 vn hax on 05.23.19 at 8:13 pm

Found this on MSN and I’m happy I did. Well written post.

#49 v9 on 05.24.19 at 8:57 am

Intresting, will come back here later too.

#50 ispoofer pogo activate seriale on 05.24.19 at 7:22 pm

Just wanna input on few general things, The website layout is perfect, the articles is very superb : D.

#51 cheats for hempire game on 05.26.19 at 7:22 am

I really enjoy examining on this website , it has got great article .

#52 iobit uninstaller 7.5 key on 05.26.19 at 10:05 am

I like this site because so much useful stuff on here : D.

#53 smart defrag 6.2 serial key on 05.26.19 at 4:42 pm

Some truly cool article on this web site , appreciate it for contribution.

#54 resetter epson l1110 on 05.26.19 at 7:31 pm

I truly enjoy looking through on this web site , it holds superb content .

#55 sims 4 seasons code free on 05.27.19 at 8:47 am

Thank You for this.

#56 rust hacks on 05.27.19 at 9:10 pm

I love reading through and I believe this website got some genuinely utilitarian stuff on it! .

#57 strucid hacks on 05.28.19 at 11:28 am

I enjoying, will read more. Thanks!

#58 expressvpn key on 05.28.19 at 8:30 pm

This i like. Cheers!

#59 ispoofer license key on 05.29.19 at 9:52 am

Enjoyed reading through this, very good stuff, thankyou .

#60 aimbot free download fortnite on 05.29.19 at 1:52 pm

Enjoyed reading through this, very good stuff, thankyou .

#61 redline v3.0 on 05.29.19 at 6:17 pm

stays on topic and states valid points. Thank you.

#62 vn hax on 05.30.19 at 7:37 am

I conceive you have mentioned some very interesting details , appreciate it for the post.

#63 xbox one mods free download on 05.31.19 at 2:07 pm

Enjoyed reading through this, very good stuff, thankyou .

#64 fortnite aimbot download on 05.31.19 at 4:49 pm

I really got into this post. I found it to be interesting and loaded with unique points of interest.

#65 mpl pro on 06.01.19 at 7:22 pm

Found this on google and I’m happy I did. Well written site.

#66 hacks counter blox script on 06.02.19 at 7:35 am

Very interesting points you have remarked, appreciate it for putting up.

#67 krunker aimbot on 06.03.19 at 11:24 am

I have interest in this, xexe.

#68 krunker aimbot on 06.17.19 at 9:11 pm

Great article to check out, glad that Yahoo took me here, Keep Up cool Work

#69 proxo key generator on 06.19.19 at 5:02 pm

Just wanna input on few general things, The website layout is perfect, the articles is very superb : D.

#70 vn hax pubg mobile on 06.21.19 at 1:28 am

Ha, here from bing, this is what i was looking for.

#71 nonsense diamond on 06.21.19 at 2:31 pm

I love reading through and I believe this website got some genuinely utilitarian stuff on it! .

#72 badoo superpowers free on 06.23.19 at 11:40 pm

You got yourself a new rader.

#73 gx tool pro apk download on 06.24.19 at 9:29 pm

Your website has proven useful to me.

#74 geometry dash 2.11 download pc on 06.26.19 at 2:20 am

Great, this is what I was looking for in bing

Leave a Comment