meaning of the construction: ${1+"$@"} ?


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users meaning of the construction: ${1+"$@"} ?
# 1  
Old 01-19-2011
meaning of the construction: ${1+"$@"} ?

Whoud anybody explain the reason to have such construction in code: ${1+"$@"}

It is used in the function that replasing functionality of the 'cd' command by processing the 'cd' itself and after that processing some other stuff:
Code:
chdir() {
   'cd' ${1+"$@"}
   ...  #other stuff
}

I understand it intends to aply function parameters to the 'cd' command.
I do not understand NEEDS for that extra-smartness.
Why not just '$*' or '$@'?
Also I am not clear about that syntaxis: I see it use the first parameter: $1, ${1}; but the '+"..."' - what is it?
If that is kind of concatenation, the "$@" - means all parameters (as set of words, I guess); so it would duplicate the first parameter: it is already mentioned before '+' and also included in $@; but, not, it is not repeating.

If someone could explain what is used here and what is the benefit from that sysntax, I will appreciate!
# 2  
Old 01-19-2011
see Table 4.2 of this link
# 3  
Old 01-19-2011
Thanks ctsgnb for that link: very usefull (I familiar with most of that from bash-man pages, but from O'Relly it is much better!)
But nothing is explained!

I see that the ${1+"$@"} would be all parameters if $1 exist or nothing, if not.

SO WHAT?!

What is the reason to have that?!!?

The '$*' or '$@' would do the same!

What the point?

Last edited by alex_5161; 01-19-2011 at 02:56 PM..
# 4  
Old 01-19-2011
I thought your question was about the ${...+...} notation.

if it is about the $* and $@, then go there

I guess it is to cover the case where the directory has some space or tab in its name or something like this, or maybe for compatible reason with some OS

oops...
Oh .... ok now i understand your question ...

What is the benefit from ${1+"$@"} notation intead of just "$@" ?
I dunnot .

Error detection on empty var may be different between $1 and $@ on some unix ??? i have no idea ... maybe having a look at the rest of the code could give some clue ?
But so far, i have no idea ... Smilie

Last edited by ctsgnb; 01-19-2011 at 02:28 PM..
# 5  
Old 01-20-2011
Once again, Thanks ctsgnb for replay!
Appreciate to see your thoughts.
... some space or tab .. - OK, reasonable to apply the $@; so it would keep the word with spase as one word!
Good point!
Anything else is not a case, as I see it here: followed code (just couple line to set the 'prompt') have no relation to first 'cd' command; all other guesses are not reasonable in here (unless the code was used from some source where other your points could be meaningfull.)

I have check situation with a space and a new line in the dir-name. Here is the test-code and results:
Code:
> typeset -f tst
tst ()
{
    cdir=$(pwd);
    echo "starting in >${cdir}< directory.";
    echo "<"${1+$@}">";
    echo "\$@ is:"$@"<";
    \cd $@;
    echo "after \\cd \$@; now in $(pwd)";
    chdir $cdir;
    \cd ${1+$@};
    echo "after \\cd \${1+\$@}; now in $(pwd)";
    chdir $cdir;
    'cd' ${1+"$@"};
    echo "after 'cd' \${1+\"\$@\"}; now in $(pwd)";
    chdir $cdir;
    \cd "$@";
    echo "after \\cd \"\$@\"; now in $(pwd)";
    chdir $cdir
}
P> mkdir TST_dir "TST dir" "TST
> dir"
>
> tst TST_dir
starting in >/export/home/dca/TMP< directory.
<TST_dir>
$@ is:TST_dir<
after \cd $@; now in /export/home/dca/TMP/TST_dir
after \cd ${1+$@}; now in /export/home/dca/TMP/TST_dir
after 'cd' ${1+"$@"}; now in /export/home/dca/TMP/TST_dir
after \cd "$@"; now in /export/home/dca/TMP/TST_dir
> tst "TST dir"
starting in >/export/home/dca/TMP< directory.
<TST dir>
$@ is:TST dir<
bash: cd: TST: No such file or directory
after \cd $@; now in /export/home/dca/TMP
bash: cd: TST: No such file or directory
after \cd ${1+$@}; now in /export/home/dca/TMP
after 'cd' ${1+"$@"}; now in /export/home/dca/TMP/TST dir
after \cd "$@"; now in /export/home/dca/TMP/TST dir
> tst "TST
> dir"
starting in >/export/home/dca/TMP< directory.
<TST dir>
$@ is:TST dir<
bash: cd: TST: No such file or directory
after \cd $@; now in /export/home/dca/TMP
bash: cd: TST: No such file or directory
after \cd ${1+$@}; now in /export/home/dca/TMP
after 'cd' ${1+"$@"}; now in /export/home/dca/TMP/TST
dir
after \cd "$@"; now in /export/home/dca/TMP/TST
dir

So, by this testing the '\cd "$@" ' is completely suffisient. Most important, it is strait forward with nothing extra; no extra functionality, no questions.

Maybe anybody see anything else that would be a reason to have 'cd' ${1+"$@"} instead of the \cd "$@" ?

Thanks!

---------- Post updated 01-20-11 at 01:08 PM ---------- Previous update was 01-19-11 at 03:53 PM ----------

I have the answer from another place: here
and this question was discussed also here2

Shortly, that syntax provide appropriate 'cd' functionality with no-arguments when "$@" for no-arguments returned empty string, instead of 'nothing' (as it was 20 years ago.)
cd - with 'nothing' change to home-dir;
cd "" - with empty string stay in current dir (so, it is the >cd . )

But today the "$@" return correctly nothing and 'cd' will process "$@" appropriately.
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 "> /dev/null 2>&1"

Hi, I am new into UNIX shell scripting and I am wondering what is the meaning of the below text which appears at the end of each line in the ".sh" file: > /dev/null 2>&1 For example, the line below: sh $HOME/stats/Rep777/Act_777.sh omc omc > /dev/null 2>&1 I know for sure what "sh... (10 Replies)
Discussion started by: salanalani
10 Replies

8. 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

9. 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
Login or Register to Ask a Question