Home > Net >  Strings and specific symbols in Bash
Strings and specific symbols in Bash

Time:12-18

I'm trying to write some commands in Bash.
I have a part of this command in hosts var.

hosts="one-1.stend.st,one-2.stend.st"
hosts="{$hosts}" # I have to "bracket" this

The command is like this:

ipa service-allow-create....... hosts=$hosts

error: {one-1.stend.st,one-2.stend.st} - no such entry

So yes, program understood brackets and all wrote but doesn't work.
When I write

ipa service-allow-create....... hosts={one-1.stend.st,one-2.stend.st}

all work as intended.

But I can't leave command like this. I have to somehow use $hosts and round it in brackets.

Help me please

CodePudding user response:

What about:

hosts="{one-1.stend.st,one-2.stend.st}"

Looks like an obvious solution. Did you try that?

CodePudding user response:

When you write

ipa service-allow-create....... hosts={one-1.stend.st,one-2.stend.st}

the shell expands that to

ipa service-allow-create....... hosts=one-1.stend.st hosts=one-2.stend.st
# ............................. ^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^

This is 3.5.1 Brace Expansion

If you look at the order of 3.5 Shell Expansions, notice that brace expansion happens before parameter expansion. You have to resort to eval to force a second round of evaluations, but that's generally not recommended.

Use an array here

myHosts=(
    one-1.stend.st
    one-2.stend.st
    two
    three
    etc
)

then use this expansion

ipa service-allow-create....... "${myHosts[@]/#/hosts=}"
  •  Tags:  
  • bash
  • Related