Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a shell variable have spaces in name?

I would like to export a variable name with spaces. Is it possible in shell export command?

{'Truststore Filename': '/hom/truststore.jks', 'Truststore Password': 'pass'}
$ export 'Truststore Password'=clientpass
-bash: export: `Truststore Password=clientpass': not a valid identifier

But i would like to export the variables using export command because we have more than more than one variable to be exported which has spaces in names.

However, the following way works.

$ env 'Truststore Password'=clientpass 'Truststore Filename'=/hom/truststore.jks python3 script.py
like image 438
SunilS Avatar asked Oct 18 '25 07:10

SunilS


1 Answers

No, this is not possible, as a variable in bash cannot have a name that includes spaces:

name

A word consisting solely of letters, numbers, and underscores, and beginning with a letter or underscore. Names are used as shell variable and function names. Also referred to as an identifier.

env, as you noticed, plays by different rules:

Environment variable names can be empty, and can contain any characters other than ‘=’ and ASCII NUL. However, it is wise to limit yourself to names that consist solely of underscores, digits, and ASCII letters, and that begin with a non-digit, as applications like the shell do not work well with other names.

(source: info '(coreutils) env invocation'; emphasis mine)

Case in point, here's another post with an answer about how much of a pain it is to grab the output of an environment variable with spaces

like image 62
jeremysprofile Avatar answered Oct 19 '25 23:10

jeremysprofile



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!