Skip to main content
added 14 characters in body
Source Link
jesse_b
  • 41.6k
  • 14
  • 108
  • 162
#!/bin/bash
var=$1
if [[ $var =~ [\`\$\=\/] || $var =~ \[ ]]; then
    echo 'no'
    exit
fi
declare "$var"=test 2>/dev/null
if [[ ${!var} == 'test' || $1 == 'var' ]]; then
    echo 'yes'
else
    echo 'no'
fi

This will try to assign the value test to the provided value. If it succeeds (if the value is a valid variable name) it will match the if test and echo yes, else it will echo no.

#!/bin/bash
var=$1
if [[ $var =~ [\`\$\=\/] ]]; then
    echo 'no'
    exit
fi
declare "$var"=test 2>/dev/null
if [[ ${!var} == 'test' || $1 == 'var' ]]; then
    echo 'yes'
else
    echo 'no'
fi

This will try to assign the value test to the provided value. If it succeeds (if the value is a valid variable name) it will match the if test and echo yes, else it will echo no.

#!/bin/bash
var=$1
if [[ $var =~ [\`\$\=\/] || $var =~ \[ ]]; then
    echo 'no'
    exit
fi
declare "$var"=test 2>/dev/null
if [[ ${!var} == 'test' || $1 == 'var' ]]; then
    echo 'yes'
else
    echo 'no'
fi

This will try to assign the value test to the provided value. If it succeeds (if the value is a valid variable name) it will match the if test and echo yes, else it will echo no.

added 2 characters in body
Source Link
jesse_b
  • 41.6k
  • 14
  • 108
  • 162
#!/bin/bash
var=$1
if [[ $var =~ [\`\$\=\/] ]]; then
    echo 'no'
    exit
fi
declare $var=test"$var"=test 2>/dev/null
if [[ ${!var} == 'test' || $1 == 'var' ]]; then
    echo 'yes'
else
    echo 'no'
fi

This will try to assign the value test to the provided value. If it succeeds (if the value is a valid variable name) it will match the if test and echo yes, else it will echo no.

#!/bin/bash
var=$1
if [[ $var =~ [\`\$\=\/] ]]; then
    echo 'no'
    exit
fi
declare $var=test
if [[ ${!var} == 'test' || $1 == 'var' ]]; then
    echo 'yes'
else
    echo 'no'
fi

This will try to assign the value test to the provided value. If it succeeds (if the value is a valid variable name) it will match the if test and echo yes, else it will echo no.

#!/bin/bash
var=$1
if [[ $var =~ [\`\$\=\/] ]]; then
    echo 'no'
    exit
fi
declare "$var"=test 2>/dev/null
if [[ ${!var} == 'test' || $1 == 'var' ]]; then
    echo 'yes'
else
    echo 'no'
fi

This will try to assign the value test to the provided value. If it succeeds (if the value is a valid variable name) it will match the if test and echo yes, else it will echo no.

added 4 characters in body
Source Link
jesse_b
  • 41.6k
  • 14
  • 108
  • 162
#!/bin/bash
var=$1
if [[ $var =~ [\`\$][\`\$\=\/] ]]; then
    echo 'no'
    exit
fi
declare $var=test
if [[ ${!var} == 'test' || $1 == 'var' ]]; then
    echo 'yes'
else
    echo 'no'
fi

This will try to assign the value test to the provided value. If it succeeds (if the value is a valid variable name) it will match the if test and echo yes, else it will echo no.

#!/bin/bash
var=$1
if [[ $var =~ [\`\$] ]]; then
    echo 'no'
    exit
fi
declare $var=test
if [[ ${!var} == 'test' || $1 == 'var' ]]; then
    echo 'yes'
else
    echo 'no'
fi

This will try to assign the value test to the provided value. If it succeeds (if the value is a valid variable name) it will match the if test and echo yes, else it will echo no.

#!/bin/bash
var=$1
if [[ $var =~ [\`\$\=\/] ]]; then
    echo 'no'
    exit
fi
declare $var=test
if [[ ${!var} == 'test' || $1 == 'var' ]]; then
    echo 'yes'
else
    echo 'no'
fi

This will try to assign the value test to the provided value. If it succeeds (if the value is a valid variable name) it will match the if test and echo yes, else it will echo no.

added 70 characters in body
Source Link
jesse_b
  • 41.6k
  • 14
  • 108
  • 162
Loading
deleted 89 characters in body
Source Link
jesse_b
  • 41.6k
  • 14
  • 108
  • 162
Loading
Source Link
jesse_b
  • 41.6k
  • 14
  • 108
  • 162
Loading