bash

Escaped asterisk (*) in list of characters outputs file in folder


At the very beginning of my Bash script I'm passing a set of characters like this:

#!/bin/bash
set0="0 1 2 3 4 5 6 7 8 9"
set1="° § + \" ç % & / ( ) = ? ' ^ ! £ $ - _ ; , : . *"

The script combines every single character with the others in order to get all possible combination with length of n.

As you can see in set1 above I have to escape the quote " character for example. My problem now is, that I have to escape the asterisk * as well. But I couldn't figure out how.

This didn't work so far:

#!/bin/bash
set1="° § + \" ç % & / ( ) = ? ' ^ ! £ $ - _ ; , : . \*"

It echoes \*. If I don't escape the asterisk * with a backslash \ the script echoes all the files in current directory all time.

Do you know how I can escape the asterisk * successfully?


This is the whole script:

  8 chars0="a b c d e f g h i j k l m n o p q r s t u v w x y z"
  9 chars1="A B C D E F G H I J K L M N O P Q R S T U V W X Y Z"
 10 chars2="0 1 2 3 4 5 6 7 8 9"
 11 chars3="° § + \" ç % & / ( ) = ? ' ^ ! £ $ - _ ; , : . *"
 12 
 13 function increment {
 14         INPUT=$1
 15         [[ ${#INPUT} -ge $minlen ]]  && echo $1
 16         if [[ ${#INPUT} -lt $maxlen ]]; then
 17                 for c in $chars; do
 18                         increment $1$c
 19                 done
 20         fi
 21 }
 22 
 23 function showUsage {
 24         echo "$0 MAXLEN [MINLEN] [a [A [n [p]]]]"
 25         echo
 26         echo "  MAXLEN  integer Maximum lenght, or specific lengt if MINLEN is omitted"
 27         echo "  MINLEN  integer Minimum lenght, equals MAXLEN if omitted"
 28         echo ""
 29         echo "  characters:"
 30         echo "  a       lower case      a-z"
 31         echo "  A       Uppercase       A-Z"
 32         echo "  n       numeric         0-9"
 33         echo "  p       punctuation     . ?" 
 34 }
 35 
 36 
 37 
 38 
 39 ## argument handler
 40 [[ "$1" = "" ]] && showUsage && exit
 41 maxlen=$1
 42 [[ $2 -eq 0 ]] && minlen=$maxlen || minlen=$2
 43 
 44 
 45 for arg in "$@"; do
 46         case $arg in
 47                 a) chars="$chars $chars0" ;;
 48                 A) chars="$chars $chars1" ;;
 49                 n) chars="$chars $chars2" ;;
 50                 p) chars="$chars $chars3" ;;
 51         esac;
 52 done
 53 
 54 #fallback
 55 [[ "$chars" = "" ]] && chars="$chars0"
 56 
 57 
 58 
 59 ## kickof
 60 
 61 for i in $chars; do
 62         increment $i
 63 done

Solution

  • Thank you for all your suggestions. The one that helped me fixing this was

    set -f
    

    It's a quick fix of course. But it works well for me.