2

I have this script I am looking at, learning scripting but I cannot figure out what this line means:

if [[ $1 = "-?" ]]

I understand the $1 is first argument, but the after the equals I cannot figure it out the -?.h

if [[ $1 = "-?" ]]
    then
    echo "Use is: 235.sh <username>"
    exit 1 
  • The question-mark after the hyphen means it is questionable the hyphen will be there. Also see [What is the meaning of a question mark in bash variable parameter expansion as in ${var?}?](https://stackoverflow.com/q/8889302/608639) – jww Feb 08 '18 at 02:52
  • 1
    are you sure it is not `if [[ $1 =~ "-?" ]]`? – Allan Feb 08 '18 at 02:54
  • @Allan no I do not see the ~ – LinuxBeginner Feb 08 '18 at 03:03
  • https://stackoverflow.com/questions/17420994/bash-regex-match-string for regular expression in if statement, usually string comparisons take single square braket! http://tldp.org/LDP/abs/html/comparison-ops.html – Allan Feb 08 '18 at 03:09
  • Possible duplicate of [What is the meaning of a question mark in bash variable parameter expansion as in ${var?}?](https://stackoverflow.com/questions/8889302/what-is-the-meaning-of-a-question-mark-in-bash-variable-parameter-expansion-as-i) – codeforester Oct 17 '18 at 18:49

2 Answers2

5

I think it would be checking to see if the first argument is a string equal to -?

0

You can check this by running:

# [[ "a" = "-?" ]] && echo true
# [[ "-a" = "-?" ]] && echo true
# [[ "a-?" = "-?" ]] && echo true
# [[ "-?" = "-?" ]] && echo true
true

I would guess that is comparing something to the string "-?".

  • 1
    Is the something the $1, or the argument? You should also be careful about globbing when running this check. –  Feb 08 '18 at 03:04
  • This is correct. The canonical flag now is `--help`, but `-?` used to be the standard and is still commonly seen. – that other guy Feb 08 '18 at 03:07