piping bash input arguments in egrep's regular expression

前端 未结 1 1055
我寻月下人不归
我寻月下人不归 2021-01-22 00:17

here is the script that I am trying to run

#!/bin/bash

charlist=$1 #possible input:cat
wordlength=$2 #possible input: 3

cat ./nounlist | egrep \\b[${charlist}]         


        
相关标签:
1条回答
  • 2021-01-22 00:39

    You need to wrap the argument in double-quotes so that Bash doesn't try to handle the backslashes, and move the second \b to after the {${wordlength}}:

    cat ./nounlist | egrep "\b[${charlist}]{${wordlength}}\b"
    

    (If you run echo \b, you'll see that it just prints b. This is because Bash assumes that the \ is there to prevent b from having any special meaning; so it removes it.)

    0 讨论(0)
提交回复
热议问题