Home > other >  how to add a space between two variables bash script
how to add a space between two variables bash script

Time:07-09

I currently have this code, options is empty String while k is the output string form a loop.

options=$options$k

How do i add a space between the variable Option and the variable k?

I want my output to look like

Option1 Option2 Option3

I have tried splitting using option=$option//$k but it gave me an error.

CodePudding user response:

You just add a space. You need to escape the space, though, to prevent the shell from parsing the assignment as two separate words

options=$options\ $k

or more idiomatically,

options="$options $k"

CodePudding user response:

Using a bash array, in your loop you can do:

options =($k)

Afterwards, by default, when you use ${options[*]} you will get a space-delimited string.


$ a =(1)
$ a =(2)
$ a =(3)
$ echo "<${a[*]}>"
<1 2 3>
  •  Tags:  
  • bash
  • Related