2

I am having trouble printing something like 10x10 in the bash script. HEre is what I have:

#!/bin/bash

i="2"
array=(1024 2048 4096 6144)

while [ $i -lt 17 ]
do
    for j in "${array[@]}"
    do
    :
    secondpart=$((j / i))
    echo "$jx$jx$j and $secondpartx$secondpart"
done
    echo "\n"
    i=$[$i*2]
done

The expected output is: 1024x1024x1024 and 512x512

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • BTW, what `echo "\n"` does when run is not well-defined -- indeed, absent XSI extensions, the POSIX standard doesn't define echo's behavior if its input contains any backslashes at all. If you just want to emit a newline, consider a bare `echo`. (The gritty details about what `echo` is and is not guaranteed to do are given at http://pubs.opengroup.org/onlinepubs/009604599/utilities/echo.html; if you review the APPLICATION USAGE section, you'll note that `printf` is, in general, the preferred/modern replacement in ambiguous situations). – Charles Duffy Mar 05 '16 at 00:34
  • Not marking as duplicate, but super closely related: http://stackoverflow.com/questions/8748831/when-do-we-need-curly-braces-in-variables-using-bash – Benjamin W. Mar 05 '16 at 01:27
  • @CharlesDuffy: Sorry about missing the exec. I have edited it. Since it is about the same stuff, I have followed up here. :( – newbiecoder Mar 05 '16 at 01:30
  • "About the same stuff"? I don't see how. Your original question is about string concatenation behavior between variable expansions and literals, and the new one is about executing code stored in strings. What do those topics have to do with each other at all? – Charles Duffy Mar 05 '16 at 01:37
  • Per prior discussion, I rolled back to revision-2, the last one before an attempted change of the question's context. Please start a new question if you want to ask about a different topic. – Charles Duffy Mar 05 '16 at 01:44

2 Answers2

3

Use ${j}x; otherwise it looks for a variable called $jx

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • A follow up question on the same code. These numbers are basically arguments to an executable. So basically: ./exec 100x100x100 10x10. How do you assign this in a string variable and then execute? I have tried cmd="./exec $jx$jx$j and $secondpartx$secondpart" and then cmd, but does not work. – newbiecoder Mar 05 '16 at 00:41
  • I have edited the code to fit this question because I realized writing here looks more nasty. So easier to explain through code – newbiecoder Mar 05 '16 at 00:45
  • @newbiecoder, just `./run-your-executable "${j}x${j}x${j}" "${secondpart}x${secondpart}"`; if you try to use `cmd='...'` to wrap the thing, you open a whole bunch of new problems that you probably don't want to deal with (see the BashFAQ link I provided previously). – Charles Duffy Mar 05 '16 at 02:47
0

Use a join type function. Much easier:

$ function join_array { local IFS="$1"; shift; echo "$*"; }
$ array=(1024 2048 4096 6144)
$ join_array x "${array[@]}"
1024x2048x4096x6144

Then to assign to a string:

$ s=$(join_array x "${array[@]}")
$ echo "$s"
1024x2048x4096x6144

Or, more directly (without the function), you can do:

$ st=$(IFS=x; echo "${array[*]}")
$ echo "$st"
1024x2048x4096x6144

If you main use is to assign to a string, use the second method.

dawg
  • 98,345
  • 23
  • 131
  • 206