Sunday, December 14, 2014

Bash Unset Readonly Variable - Hack

Found on the web at: 
(http://stackoverflow.com/questions/17397069/unset-readonly-variable-in-bash)  
E.g. Accidentally a READONLY variable is declared at Bash shell prompt
Here is the test script commands:   
declare -r RESTORE_BASHNOCASEMATCH="$(shopt -p nocasematch)"
echo "$RESTORE_BASHNOCASEMATCH"
unset RESTORE_BASHNOCASEMATCH
cat << EOF | sudo gdb -silent
attach $$
call unbind_variable("RESTORE_BASHNOCASEMATCH")
detach
EOF
echo "$RESTORE_BASHNOCASEMATCH"
unset RESTORE_BASHNOCASEMATCH 
Here is the result:   
$ declare -r RESTORE_BASHNOCASEMATCH="$(shopt -p nocasematch)"
$ echo "$RESTORE_BASHNOCASEMATCH"
shopt -u nocasematch
$ unset RESTORE_BASHNOCASEMATCH
bash: unset: RESTORE_BASHNOCASEMATCH: cannot unset: readonly variable
$ cat << EOF | sudo gdb -silent
> attach $$
> call unbind_variable("RESTORE_BASHNOCASEMATCH")
> detach
> EOF
(gdb) Attaching to process 5207
Reading symbols from /bin/bash...(no debugging symbols found)...done.
Reading symbols from /lib/x86_64-linux-gnu/libtinfo.so.5...(no debugging symbols found)...done.
 .... 
(gdb) Detaching from program: /bin/bash, process 5207
(gdb) quit 
$ echo "$RESTORE_BASHNOCASEMATCH"
$ unset RESTORE_BASHNOCASEMATCH
$  
No more errors, it was a success! 

No comments:

Post a Comment