1

I am trying to set this environment variable with semicolons on ubuntu :

BS_DATABASE_CONNECTION=connection;Data Source=source;Initial Catalog=catalog;Persist Security Info=info;User ID=userid;Password=password;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;

But i get these errors:

Data: command not found

Initial: command not found

Persist: command not found

User: command not found

How do i set this correctly?

GrimyCanga
  • 21
  • 3
  • 1
    I think the issue is the spaces between your variable names. Your first variable name did it fine `BS_DATABASE_CONNECTION` but then you called the next variable `Data Source` so bash thinks `Data` is a command and `Source` is the variable. Try `Data_Source` or something with no spaces. Same for all the errors – wxz May 12 '21 at 15:00

1 Answers1

3

The problem is that ; is treated as a control operator, and the string BS_DATABASE_CONNECTION=connection;Data Source=source;... is interpreted as multiple commands. The first is the command BS_DATABASE_CONNECTION=connection, the second is the command Data Source=source, etc. One solution is to use quotes:

BS_DATABASE_CONNECTION='connection;Data Source=source;Initial Catalog=catalog;Persist Security Info=info;User ID=userid;Password=password;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;'

But with a value this long, it would probably be more readable to do something like:

BS_DATABASE_CONNECTION="\
connection;\
Data Source=source;\
Initial Catalog=catalog;\
Persist Security Info=info;\
User ID=userid;\
Password=password;\
MultipleActiveResultSets=False;\
Encrypt=True;\
TrustServerCertificate=False;\
"

or

BS_DATABASE_CONNECTION=''
for x in 'connection' \
        'Data Source=source' \
        'Initial Catalog=catalog' \
        'Persist Security Info=info' \
        'User ID=userid' \
        'Password=password' \
        'MultipleActiveResultSets=False' \
        'Encrypt=True' \
        'TrustServerCertificate=False' \
; do
        BS_DATABASE_CONNECTION="${BS_DATABASE_CONNECTION}${x};"
done

or

read BS_DATABASE_CONNECTION << EOF
connection;\
Data Source=source;\
Initial Catalog=catalog;\
Persist Security Info=info;\
User ID=userid;\
Password=password;\
MultipleActiveResultSets=False;\
Encrypt=True;\
TrustServerCertificate=False;
EOF

or

BS_DATABASE_CONNECTION=$( tr \\n \; << EOF
connection
Data Source=source
Initial Catalog=catalog
Persist Security Info=info
User ID=userid
Password=password
MultipleActiveResultSets=False
Encrypt=True
TrustServerCertificate=False
EOF
)

Note that this is not an environment variable. It is merely a shell variable. If you want it to be in the environment of subshells, you should export it. Whether it is in the environment of the shell or not should be completely irrelevant, and there is no functional difference between an environment variable and an exported shell variable.

William Pursell
  • 204,365
  • 48
  • 270
  • 300
  • You don't need the for loops. Adjacent quoted strings are automatically concatenated, even with multi-line backslash continuations (so long as there's no space after the closing quote and the backslash, or at the beginning of the following line). – Jim Stewart May 12 '21 at 16:36
  • I also like `printf -v` for assigning complicated values to variables. With `printf -v var %s 'part1' 'part2 'part3'` you can have arbitrary whitespace and backslashed newlines etc outside the quoted strings, but the end result will just be those strings smashed together. – tripleee May 12 '21 at 19:16