2016-12-27 6 views
6

Ich habe keine Ahnung, warum die folgende Zeile 1, während die nachfolgenden Ausführungen von ((count++)) zurückkehrt sind 0.Warum ((count ++)) return 1 Exit-Code erstes Mal läuft

[[email protected] ~]$ count=0 
[[email protected] ~]$ echo $? 
0 
[[email protected] ~]$ count++ 
-bash: count++: command not found 
[[email protected] ~]$ (count++) 
-bash: count++: command not found 
[[email protected] ~]$ ((count++)) 
[[email protected] ~]$ echo $? 
1 <------THIS WHY IS IT 1 AND NOT 0?? 
[[email protected] ~]$ ((count++)) 
[[email protected] ~]$ echo $? 
0 
[[email protected] ~]$ ((count++)) 
[[email protected] ~]$ echo $? 
0 
[[email protected] ~]$ echo $count 
3 

Antwort

10

die excerpt Siehe Rückkehr aus die Seite,

Wenn das letzte ARG 0 ergibt, geben Sie 1 zurück; 0 wird andernfalls zurückgegeben.

Da der Betrieb ist Nachinkrement, ((count++)), zum ersten Mal 0 beibehalten wird, damit 1

Hinweis, das gleiche geschieht nicht für Prä-Inkrement ((++count)), da der Wert zurückzukehr zu 1, auf der ersten Iteration selbst.

$ unset count 
$ count=0 
$ echo $? 
0 
$ ++count 
-bash: ++count: command not found 
$ echo $? 
127 
$ ((++count)) 
$ echo $? 
0 
Verwandte Themen