I need to Concatenat string with leading dash in multiple choice to variable, but it doesn't work as expected and I don't know what is the problem
#!/bin/bash
filterOptionsStr=""
PS3='Filter Options: '
options=("Filter HTTP status codes from response"
"skip/quit")
select option in "${options[@]}"; do
case $option in
"Filter HTTP status codes from response")
echo $option
read -ep "Status codes: " status_codes
filterOptionsStr+= "-fc ${status_codes} "
echo $filterOptionsStr
;;
"skip/quit")
break
;;
esac
done
output:
└─$ ./test 130 ⨯
1) Filter HTTP status codes from response
2) skip/quit
Filter Options: 1
Filter HTTP status codes from response
Status codes: 301
./test: line 16: -fc 301: command not found
Filter Options: 1
Filter HTTP status codes from response
Status codes: 404
./test: line 16: -fc 404: command not found
Filter Options:
=
and the quote. When there's a space there, the shell is appending the empty string to the filterOptionsStr variable, and then attempting to execute "-fc $status_codes" as a command – glenn jackman Jun 07 '22 at 18:16