Setting an environment variable with a leading digit in bash

I need to set an environment variable called "64 bit" (ie with a leading digit) in bash. However, bash variable names forbid a leading digit variable. I know a way to install it when calling bash:

env 64bit=1 /usr/bin/bash 

However, I am looking for a way to change it in the current working shell, that is, without starting a new shell. I also know that csh allows variables to start with numbers, but I need to use bash.

Is there any way to achieve this?

+4
source share
2 answers

You can also bypass the bash interpreter and define the variable directly using the bash internal functions:

 $ gdb --batch-silent -ex "attach $$" \ -ex 'set bind_variable("64bit", "1", 0)' \ -ex 'set *(int*)(find_variable("64bit")+sizeof(char*)*5) = 1' \ -ex 'set array_needs_making = 1' $ env | grep 64 64bit=1 
+4
source

As people point out, Bash does not allow variables starting with numbers. However, it passes an unrecognized environment string to external programs, so the variable maps to env , but not to set .

As a workaround, you can work with a valid name like _64bit , and then automatically enter your invalid variable name in the commands that you run:

 #!/bin/bash # Setup for injection hack original=$PATH PATH="/" command_not_found_handle() { PATH="$original" env "64bit=$_64bit" " $@ " } # Your script and logic _64bit="some dynamic value" # This verifies that '64bit' is automatically set env | grep ^64bit 

Note that this particular method only works when called through $ PATH, and not when using relative or absolute path names.

If you are calling by path name, consider changing PATH and calling by name.

+1
source

All Articles