Midhun Agnihotram Midhun Agnihotram - 1 month ago 7
Bash Question

In UNIX Shell Scritpting, what does "eval XYZ='$'${1,,}" do?

In UNIX Shell Scritpting, what does this do?

eval XYZ='$'${1,,}


I looked up the parameter substitution page for UNIX which has a lot of options for setting up defaults, patterns etc. But I couldn't find anything relevant.

123 123
Answer

Say we pass Shell as $1 to the script:

./script.sh Shell

Then the following would occur


${1,,}

Changes all characters in $1 to lowercase.

So we now have the string

shell

'$'

Is a literal dollar

So we now have the string

$shell

XYZ=

Is an assignment, but in this case is just a string before it is evaluated

So we now have the string

XYZ=$shell

eval 

Evaluates the following expression.

So eval executes the above string as

XYZ=$shell

The whole command sets XYZ to equal the value of a variable with the name of lowercase $1