0

I have a bash string variable:

switches="-r cc -c 1,2,3,4 -u"

where numbers 1,2,3,4 can be any integer, like:

-c 25,45,78,34.

Moreover, it can be with fewer numbers, like:

-c 1

-c 1,2

or

-c 1,2,3,4

It can't be like: -c 1,2,3

So -c can have one, two, or four integers only.

I forgot to mention that that this pattern can appeares also at the beginning, or at the end of the string variable $switches too, like:

-r cc -u -c 1,2,3,4

-r cc -u -c 1,2,3

And one more thing: this pattern can be appeared in the $switches variable only once.

How can I remove the '-c 1,2,3,4 ' part of switches variable using just bash? I tried with this:

switches=${switches/ -c /}

but get this:

-r cc1,2,3,4 -u

I expect this:

-r cc -u

Best, Pal

5
  • 1
    You are already doing it wrong in the first line. switches should be an array, not a regular variable: switches=(-r cc -c 1,2,3,4 -u). Commented Jan 21, 2018 at 0:00
  • The switches array could have more then those three values: (-r cc), (-c 1,2,3,4), (-u). switches array can have maximum 17 string values. Commented Jan 21, 2018 at 6:13
  • The switches array could have more then those three values: (-r cc), (-c 1,2,3,4), (-u). switches array can have maximum 17 string values. (-r cc) is one value in the array, (-c 1,2,3,4) or (-c 1) or (-c 1,2) is another value, (-u) is another value, (-g) is another, and so on. In this case switches=(-r cc -c 1,2,3,4 -u) is not the proper way for creating the switches array, right? Commented Jan 21, 2018 at 6:20
  • Then it should be created like this: switches=('-r cc' '-c 1,2,3,4' '-u' '-g'). But then how can I remove a value of that array, because value '-c 1,2,3,4' cod vary like this: '-c 1' '-c 1,2' '-c 1,2,3,4' ? Commented Jan 21, 2018 at 7:01
  • OK, I can arrange so so in switches array every values goes in to right place, like this: switches=('-r cc' '-l l' '-c 1,2,3,4' '-g' '-e none' '-f 15' '-o 5' '-u' '-t 50' '-s 0' '-S 200' '-a 0' '-p 0' '-T' '-b white' '-F 10' '-i 1') so eg. '-t 50' could have always the index number of 8. So how can then remove such a value from the switches array? Commented Jan 21, 2018 at 7:33

1 Answer 1

1

Using extglob:

shopt -s extglob                         # enables extended globbing
switches=${switches//-c *([^ ])}
  • *([^ ]): matches any number of non-spaces

This will leave you with unnecessary spaces. More complicated solution:

switches=${switches//-c *([^ ])*( )}
switches=${switches/%*( )}
  • *([^ ])*( ): matches any number of non-spaces and any number of spaces after
  • ${switches/%*( )}: if the last option is also -c, the code above wouldn't remove the spaces left by it. /%*( ) removes any number of spaces from the end
Sign up to request clarification or add additional context in comments.

1 Comment

This code did it: switches=${switches//-c *([^ ])*( )}. Thank you very much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.