In the Perl documentation, perlrun(1) suggests launching Perl scripts using a bilingual shell/Perl header:
#!/bin/sh
#! -*-perl-*-
eval 'exec perl -x -wS $0 ${1+"$@"}'
if 0;
What does ${1+"$@"}
mean? I tried using "$@"
instead (using Bash as /bin/sh), and it seems to work just as well.
Edit
Two answers below say that it's supposed to be ${1:+"$@"}
. I am aware of the ${parameter:+word}
("Use Alternate Value") syntax documented in bash(1). However, I am unconvinced, because
Both
${1+"$@"}
and"$@"
work just fine, even when there are no parameters. If I create simple.sh as#!/bin/sh eval 'exec /usr/bin/perl -x -S -- $0 "$@"' if 0; #!perl use Data::Dumper; print Dumper(\@ARGV);
and question.sh as
#!/bin/sh eval 'exec /usr/bin/perl -x -S -- $0 ${1+"$@"}' if 0; #!perl use Data::Dumper; print Dumper(\@ARGV);
I can get both to work identically:
$ ./question.sh $VAR1 = []; $ ./question.sh a $VAR1 = [ 'a' ]; $ ./question.sh a 'b c' $VAR1 = [ 'a', 'b c' ]; $ ./question.sh "" $VAR1 = [ '' ]; $ ./simple.sh $VAR1 = []; $ ./simple.sh a $VAR1 = [ 'a' ]; $ ./simple.sh a 'b c' $VAR1 = [ 'a', 'b c' ]; $ ./simple.sh "" $VAR1 = [ '' ];
Other sources on the Internet also use
${1+"$@"}
, including one hacker who seems to know what he's doing.
Perhaps ${parameter+word}
is an undocumented alternate (or deprecated) syntax for ${parameter:+word}
? Could someone confirm that hypothesis?
${1+"$@"}
so some places switch to"$@"
on zsh. example – 7efkvNEq Dec 28 '21 at 12:30