Creating variables in bash

I am writing some scripts using bash and am wondering if there is a better way to perform the following set of formatting variables.

s1=" "
s2="  "
s3="   "
s4="    "
s5="     "
s6="      "
s7="       "
s8="        "
 
frmt_titl="${bYl}%s${nClor}\n"

frmt1_titl="${s1}$frmt_titl"
frmt2_titl="${s2}$frmt_titl"
frmt3_titl="${s3}$frmt_titl"
frmt4_titl="${s4}$frmt_titl"
frmt5_titl="${s5}$frmt_titl"
frmt6_titl="${s6}$frmt_titl"
frmt7_titl="${s7}$frmt_titl"
frmt8_titl="${s8}$frmt_titl"

nfrmt_titl="\n$frmt_titl"
nfrmt1_titl="\n$frmt1_titl"
nfrmt2_titl="\n$frmt2_titl"
nfrmt3_titl="\n$frmt3_titl"
nfrmt4_titl="\n$frmt4_titl"
nfrmt5_titl="\n$frmt5_titl"
nfrmt6_titl="\n$frmt6_titl"
nfrmt7_titl="\n$frmt7_titl"
nfrmt8_titl="\n$frmt8_titl"

Try this:

$ w=12; printf "%${w}s\n" hello
       hello
$ w=20; printf "%${w}s\n" hello
               hello

It looks interesting. I will be doing something as shown below:

  printf "$frmt4_titl" "NAME"
 

which gives yellow color text with 4 spaces in the front

I do not want to have a long line. The idea is that I write relatively short format string, followed by the things I want to print.

I essence, I want to construct the format variables above in a better way.

---------- Post updated at 03:26 PM ---------- Previous update was at 03:22 PM ----------

Can I construct a variable name with a counter using a loop?

---------- Post updated at 03:34 PM ---------- Previous update was at 03:26 PM ----------

I have been thinking a bit more and variable names in shell are not designed to be dynamically created. So I suppose I would need to keep the code not much different.

You can use eval (or indirect references in bash/ksh93), but you need to be careful with security, especially with user input or input files...

$ for i in 1 2 3; do eval var$i=$((i*10)); done
$ echo $var1 $var2 $var3
10 20 30

What do you think of using

export

Example

i=4
s4="    "
frmt_titl="${bYl}%s${nClor}\n"
export frmt${w}_titl="${s4}$frmt_titl"

---------- Post updated at 03:46 PM ---------- Previous update was at 03:44 PM ----------

I am planning to define all this stuff in a bash file. Then source it, so that I can use the variables from another script.

---------- Post updated at 03:49 PM ---------- Previous update was at 03:46 PM ----------

cat utility.bash

#!/bin/bash
for i in 1 2 3; do eval var$i=$((i*10)); done
cat test.bash

#!/bin/bash
source utility.bash
echo "$var1"

[/CODE]

What's wrong with using printf? You can tell it how wide to make a field, instead of stuffing 20 spaces in one string, 19 in another, 18 in another...

There is nothing wrong with using printf. In fact I will use it. What I am trying to do is specify the format to printf. Then in my main script I call printf followed by the format string, followed by the strings to output. Else things will get too long if I put the actual format.

Example

printf "format" "str1" "str2" "str3" "str4" "str5"

The thing is that the output of str1 str2 str3 might also be of different colors which makes the format specification very long

As an example, the example might print as

str1, str2, str3, str4 str5

I would create a function for that

How would this function work exactly?

Well something like this to give you an idea:

RED="\e[0;31m"
BLUE="\e[0;34m"
GREEN="\e[0;32m"
NOCOLOR="\e[0m"

formatprint() {
  printf "${3}%${2}s\n${NOCOLOR}" "$1"
}

formatprint "Hello World"
formatprint "Hello World" 16
formatprint "Hello World" 24
formatprint "Hello World" 30 "$RED"
formatprint "Hello World" 36 "$GREEN"

If you are using bash 4 or ksh93 (my favorite for shell scripts) you can use associative array elements for the colors instead of variables

1 Like

Thank you for this. Associative arrays are very nice, but cannot use them when running my scripts on some clusters I have available, which is a pity.

---------- Post updated at 05:45 AM ---------- Previous update was at 05:12 AM ----------

As an addition, I am constructing some mnemonic keywords from which I can quickly extract information. I use three attributes: s, v, and d.

Mnemonic examples:

1s             one option
2s             two options
3s             three options
1sv           one option with value
1s2v         one option with two values
2sv           two options with value
2svd         two options with value and description

I want to determine the total number of attributes in the string as follows:

1s              nAtrb=1
2s              nAtrb=2
3s              nAtrb=3
1sv            nAtrb=2
1s2v          nAtrb=3
2sv            nAtrb=3
2svd          nAtrb=4