tl;dr ... found a script that uses ${1+"$@"}
to forward command line arguments to another script. What exactly does this do? When do you use it instead of $@
and "$@"
?
There's a simple shell script called runhaskell
that is distributed with ghc on Ubuntu 15.10 (and perhaps others). I am trying to figure out how to replace it with something that's aware of cabal sandboxes. The shell script looks like this. It appears to have some variables that aren't used and an unnecessary shebang-like comment (from an earlier version of the script?)
#!/bin/bash
exedir="/usr/lib/ghc/bin"
exeprog="runghc"
executablename="$exedir/$exeprog"
datadir="/usr/share"
bindir="/usr/bin"
topdir="/usr/lib/ghc"
#!/bin/sh
exec "$executablename" -f "$bindir/ghc" ${1+"$@"}
I'm having trouble understanding the last line. /usr/lib/ghc/bin/runghc
is a native executable that runhaskell
evidently delegates to, and one of the arguments it passes in is the location of the compiler. Okay, no problem.
What does ${1+"$@"}
do and when should you use it? My best guess is that 1
is just a test that always succeeds and that this construction is just being used just to pass in the arguments verbatim. I remember POSIX requires that $@
and "$@"
behave somewhat differently from what you'd expect, but I don't remember the exact details.
${1+"$@"} site:unix.stackexchange.com
– don_crissti Mar 13 '16 at 20:20