Significance of ':-' while accessing a variable | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Significance of ':-' while accessing a variable

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 01-20-2013
zulfi123786 zulfi123786 is offline
Registered User
 
Join Date: Jan 2012
Last Activity: 19 May 2014, 6:10 AM EDT
Posts: 18
Thanks: 1
Thanked 0 Times in 0 Posts
Significance of ':-' while accessing a variable

Hi

I was trying to understand what ':-' means when used with variables


Code:
 
echo ${x:-10}
 
if [ "${OSH_compileFlag:-}" = "1" ]

Thanks
Sponsored Links
    #2  
Old 01-20-2013
Jotne's Avatar
Jotne Jotne is offline
Registered User
 
Join Date: Dec 2010
Last Activity: 20 September 2014, 2:08 AM EDT
Posts: 1,040
Thanks: 62
Thanked 216 Times in 204 Posts
I do not know if -10 is a valid argument, i thought it needs a positive integer.


Code:
echo ${x:a:b}
a=start
b=number to select


Code:
x=1234567890
echo ${x:2}
34567890

echo ${x:2:4}
3456

Sponsored Links
    #3  
Old 01-20-2013
zulfi123786 zulfi123786 is offline
Registered User
 
Join Date: Jan 2012
Last Activity: 19 May 2014, 6:10 AM EDT
Posts: 18
Thanks: 1
Thanked 0 Times in 0 Posts
The above is actually creating the variable with value 10 and the variable exists for only one time use. The second attempt to access the variable says it isn't defined (blank value)
    #4  
Old 01-20-2013
Scrutinizer's Avatar
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 31 October 2014, 5:50 PM EDT
Location: Amsterdam
Posts: 9,562
Thanks: 286
Thanked 2,430 Times in 2,177 Posts
It means:

Code:
${parameter:-word}
    Use Default Values. If parameter is unset or null, the expansion of word shall be substituted; otherwise, the value of parameter shall be substituted.

See Shell Command Language: 2.6.2 Parameter Expansion
The Following 2 Users Say Thank You to Scrutinizer For This Useful Post:
karthikeayan (01-22-2013), zulfi123786 (01-20-2013)
Sponsored Links
    #5  
Old 01-20-2013
zulfi123786 zulfi123786 is offline
Registered User
 
Join Date: Jan 2012
Last Activity: 19 May 2014, 6:10 AM EDT
Posts: 18
Thanks: 1
Thanked 0 Times in 0 Posts
Many Thanks for mentioning the reference
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
accessing variable from while loop free2rhyme2k Shell Programming and Scripting 3 04-20-2012 09:20 AM
Accessing local variable IND123 Shell Programming and Scripting 3 06-09-2011 02:39 AM
Accessing a variable from another file abhinav192 Shell Programming and Scripting 0 11-23-2009 10:36 AM
accessing a variable or array of one script in another tprayush Shell Programming and Scripting 3 10-27-2009 01:17 PM
Accessing a variable from another script little_wonder Shell Programming and Scripting 2 06-24-2009 05:17 AM



All times are GMT -4. The time now is 07:35 PM.