我需要在bash中设置一个名为“64bit”的环境变量(即以数字开头),但是bash变量名不允许以数字开头。我知道一种在调用bash时设置它的方法:
env 64bit=1 /usr/bin/bash
然而,我正在寻找一种方法来更改当前正在运行的shell中的变量名,即不通过启动新的shell。我也知道csh允许变量以数字开头,但我需要使用bash。
有没有办法实现这个目标?
我需要在bash中设置一个名为“64bit”的环境变量(即以数字开头),但是bash变量名不允许以数字开头。我知道一种在调用bash时设置它的方法:
env 64bit=1 /usr/bin/bash
然而,我正在寻找一种方法来更改当前正在运行的shell中的变量名,即不通过启动新的shell。我也知道csh允许变量以数字开头,但我需要使用bash。
有没有办法实现这个目标?
您也可以通过绕过bash解释器并使用bash内部函数直接定义变量:
$ 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
env
中而不在 set
中的原因。_64bit
,然后自动将无效的变量名注入到您运行的命令中。#!/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
bash
shell变量,这些变量不能以数字开头。 - chepnerenv
更加宽松的原因。 - chepner