what is +$ in this command: [[ $1 =~ ^[0-9]+$ ]]
            Asked
            
        
        
            Active
            
        
            Viewed 48 times
        
    -4
            
            
        - 
                    Sure that's not `[[ ... ]]`? – Shawn Apr 20 '20 at 03:22
- 
                    sorry I forget to put that.. Could you please tell me why +$ should be added? – Prajun Lungeli Apr 20 '20 at 03:24
2 Answers
2
            
            
        The + applies to the [0-9] and not the $.
The intended command was:
[[ $1 =~ ^[0-9]+$ ]]
It checks if $1 only contains digits, e.g. 123 or 9 (but not 123f or foo or empty string).
It breaks down as:
- [[, start of a Bash extended test command
- $1, the first parameter
- =~, the Bash extended test command regex match operator
- ^[0-9]+$, the regex to match against:- ^, anchor matching the start of the line
- [0-9]+, one or more digits- [0-9], a digit
- +, one or more of the preceding atom
 
- $, anchor matching the end of the line
 
- ]]to terminate the test command
 
    
    
        that other guy
        
- 116,971
- 11
- 170
- 194
1
            
            
        + in regexp matches for "1 or more times the preceding pattern" and $ signifies the end of string anchor. 
^ is beginning of string anchor (the natural complement to $), and [0-9] matches any single digit (in the range of 0 to 9).
 
    
    
        JGof
        
- 139
- 5
