meaning of ${1+"$@"}


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting meaning of ${1+"$@"}
# 1  
Old 05-22-2011
meaning of ${1+"$@"}

Can someone explain to me meaning of
Code:
${1+"$@"}

in a shell script?
Thank you.
# 2  
Old 05-22-2011
It was used as a workaround for bugs in older versions of the Bourne shell.
As far as I know, the buggy versions substituted "$@" with a single empty argument (instead of nothing) when no arguments were provided.

The ${1+"$@"} is evaluated as follows: if the first parameter is set, then substitute "$@", else nothing.
This User Gave Thanks to radoulov For This Post:
# 3  
Old 05-22-2011
Ahhh... that's how it is. Thanks for the explanation.
I tried to look for it in 'parameter expansion' section of 'man bash', but on my machine (GNU bash, version 4.1.5) there is only
Code:
${parameter:+word}

, explained as:
Quote:
Use Alternate Value. If parameter is null or unset, nothing is substituted, otherwise the expansion of word is substituted.
which is equivalent to what you say ${parameter+word} does. I tested the ${param+word} and ${param:+word} and the difference is in the case when 'param' is null:
Code:
$ cat doEcho.sh
#!/bin/bash
param=""
var=${param:+"Param has been set"}
echo Var: $var
$ ./doEcho.sh
Var:
$ cat doEcho.sh
#!/bin/bash
param="blah"
var=${param:+"Param has been set"}
 echo Var: $var
$ ./doEcho.sh 
Var: Param has been set

No colon:
Code:
$ cat doEcho.sh
#!/bin/bash
param=""
var=${param+"Param has been set"}
echo Var: $var
$ ./doEcho.sh
Var: Param has been set
$ cat doEcho.sh
#!/bin/bash
param="blah"
var=${param+"Param has been set"}
echo Var: $var
$ ./doEcho.sh 
Var: Param has been set

With positional parameter, e.g. ${1+"word"}, the colon seems to make no difference.

Last edited by mirni; 05-22-2011 at 08:31 AM..
# 4  
Old 05-22-2011
Quote:
Originally Posted by mirni
[...]
With positional parameter, e.g. ${1+"word"}, the colon seems to make no difference.
I believe it has the same effect (null vs. not null) with positional parameters:

Code:
#!/bin/bash

for a; do
  printf '%-5s => %s <-> %s\n' "$a" "${a+set (null or not null)}" "${a:+set and not null}"
done

Code:
zsh-4.3.11[t]% ./s '' 1
      => set (null or not null) <-> 
1     => set (null or not null) <-> set and not null


Last edited by radoulov; 05-22-2011 at 03:46 PM..
# 5  
Old 05-22-2011
Every sh, ksh, or bash man page I have seen documents that colon. From the bash man page:
Quote:
Omitting the colon results in a test only for a parameter that is unset.
# 6  
Old 05-23-2011
Indeed, upon a better look, i found the colon documented in bash manual pages... my bad. Also positional params work, too. I guess I was hasty with my statements :-|
Thank you for corrections.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. UNIX for Dummies Questions & Answers

What is the meaning of "-s" option in "if" statement?

Hi Guys, I'm sorry but I can't find answer for this, what is the meaning of -s option in "if" statement on unix scipting. Please see sample below: opath=/home/output for i in N1 N2 N3 N4 do echo $i if then grep $i $opath/N5_CRAI > $opath/N5_$i.crai chmod 777 $opath/N5_$i.crai ... (7 Replies)
Discussion started by: rymnd_12345
7 Replies

3. UNIX for Dummies Questions & Answers

Variable "##*", "% *" meaning

Hi, What means "##*", "% *" in a variable?? I have this in the script that i'm reading: ... read line echo $line echo ${line#* } echo ${line##* } echo ${line% * } ... The first print: DN: RCROOT ONRM_ROOT_MO SNW ONRM_ROOT_MO BSC BSCCC2 BTS ALTOHATILLONOR The second print:... (2 Replies)
Discussion started by: darocham
2 Replies

4. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

5. Shell Programming and Scripting

Meaning of "b" modifier in "sort" command

I need to sort the following file by the rhdiskpower devices in the last column: Total_MB Free_MB OS_MB Name Failgroup Library Label UDID Product Redund Path 1024 851 1024 OCRVOT1_0000 OCRVOT1_0000 System UNKNOWN ... (3 Replies)
Discussion started by: wjssj
3 Replies

6. UNIX for Dummies Questions & Answers

the meaning of "!:*" in "alias foo 'command\!:*' filename"

Hi: How can I remove my own post? Thanks. (2 Replies)
Discussion started by: phil518
2 Replies

7. Shell Programming and Scripting

Meaning of "if [ -x /opt/OV/bin/ovpolicy ]"

Hi, Could someone pls help me on the below command: if then ------------------ ------------ fi if then ------------- ------------------- ------------------- fi What does this signify? Thanks, .. (4 Replies)
Discussion started by: ww26683
4 Replies

8. UNIX for Dummies Questions & Answers

Meaning of $var->{"@$row[0]"}=" "; ???

while (my $row = $sth->fetchrow_arrayref) { $var->{"@$row"}=" "; } Can anyone help me understanding above mentioned. i) As per my knowledge $row is taking ARRAY Refernce from the database ii) @$row is containing the value of 0th index of the array, testted the same. but I am not able... (0 Replies)
Discussion started by: jaigs_27
0 Replies

9. AIX

What's meaning of "POWER" in diag command.?

I ran 'diag' command to verify disk follow the below steps. DIAG------?task selection-------ssa service aid-----Link verification I found that there are 3 types of status. (Good, Failed and Power). scp2: pdisk23 D69E584F 0 16 Power scp2: pdisk19 D657E7A9 1 15 Power scp2: pdisk28... (1 Reply)
Discussion started by: pattarapongn
1 Replies
Login or Register to Ask a Question