What is ${#1}?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting What is ${#1}?
# 1  
Old 03-31-2015
What is ${#1}?

What is the purpose of ${#1} in a shell script?

if [[ ${#1} -le 0 || ${#2} -le 0 ]]; then
# 2  
Old 03-31-2015
${1} would be the first parameter.

${#1} would be the length of the first parameter.

It's testing if the first two arguments are empty. I'd have just done [ -z "$1" ] || [ -z "$2" ] but whatever works.
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 03-31-2015
man ksh yields:
Code:
       ${#parameter}
              Parameter length.  The length in characters of the value of parameter is substituted.  If param‐
              eter is * or @, the value substituted is the number of positional parameters.  If  parameter  is
              an  array  name  subscripted  by  * or @, the value substituted is the number of elements in the
              array.  If parameter is an indexed array name subscripted by a negative number, that  number  is
              interpreted  as relative to one greater than the maximum index of parameter, so negative indices
              count back from the end of the array, and an index of -1 references the last element.

# 4  
Old 03-31-2015
Code:
if [ ! "$1$2" ]; then
     echo "both are empty"
fi

This User Gave Thanks to cjcox For This Post:
# 5  
Old 03-31-2015
Thank you all for the response. The information definitley helped.
Login or Register to Ask a Question

Previous Thread | Next Thread
Login or Register to Ask a Question