LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Why do I need a space here? (https://www.linuxquestions.org/questions/programming-9/why-do-i-need-a-space-here-4175413671/)

shadyabhi 06-27-2012 10:36 AM

Why do I need a space here?
 
Is there a reason behind requiring a space?
Or at lease a logic behind why was it done like that?

Code:

shadyabhi@MBP-archlinux ~ $ array=( zero one two three four five )
shadyabhi@MBP-archlinux ~ $ echo ${array[@]:-1:1}
zero one two three four five
shadyabhi@MBP-archlinux ~ $ echo ${array[@]: -1:1}
five
shadyabhi@MBP-archlinux ~ $

I observed above that if I don't give a space between "]:" and "-1", something troublesome happens.

Snark1994 06-27-2012 11:01 AM

http://wiki.bash-hackers.org/syntax/...ring_expansion, scroll down to "Negative offset value".

Essentially, otherwise the ':-' is interpreted as the syntax for a default value:

Code:

$ echo ${undefined_variable:-1:1}
1:1

You could also put brackets around the (-1) instead of putting a space before it.

shadyabhi 06-27-2012 11:08 PM

Quote:

Originally Posted by Snark1994 (Post 4713398)
http://wiki.bash-hackers.org/syntax/...ring_expansion, scroll down to "Negative offset value".

Essentially, otherwise the ':-' is interpreted as the syntax for a default value:

Thanks Snark1994. Also, the website looks like a great resource and interesting to read too. I always used to go to TLDP and search for stuff but this one seems more interesting.


All times are GMT -5. The time now is 02:04 AM.