#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    245
    Rep Power
    12

    Interpreting Bash command firstchar=${1:0:1}


    Interpreting Bash command firstchar=${1:0:1}

    1) What does the code on line 8) firstchar=${1:0:1} do ?
    2) $ stand for an argument, but what does {1:0:1} do ?

    =========================
    Code:
    1) if [[ $# -eq 2 ]]
    2) then
    3)   if [[ "$1" != "$2" ]]
    4)   then
    5)    exit 1 
    6)   fi
    7) else
    8)  firstchar=${1:0:1}
    9)  if [[ "$firstchar">"/" && "$firstchar"<":" ]]
    10)  then
    11)   exit 2   # username cannot start with a number
    12)  fi
    13) fi
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2010
    Posts
    1
    Rep Power
    0
    Answers:

    1) It puts the first character of $1 in $firstchar.
    2) ${1:0:1} expands to a substring of $1 starting at character 0 and getting 1 character. So the 0 is the starting index of the substring and the 2nd 1 is the number of characters in the substring.

IMN logo majestic logo threadwatch logo seochat tools logo