Variable passed as argument

I have a script.

#!/bin/sh

cur_$1_modify_time=Hello
echo "cur_$1_modify_time"

When I run like

sh /root/script1 jj

I expect value "Hello" being assigned to variable "cur_jj_modify_time" and output being "Hello" ie echoing $cur_jj_modify_time

But the output comes as

# sh /root/script1 jj
/root/script1: line 3: cur_jj_modify_time=Hello: command not found
cur_jj_modify_time

Please help.

#!/bin/sh

eval cur_$1_modify_time=Hello
echo "cur_$1_modify_time"

http://www.unix.com/man-page/posix/1posix/eval/

1 Like

anbu23's answer does not produce the desired output, i.e. hello.

Try this:

#!/bin/bash

eval cur_$1_modify_time=Hello
eval fpm=$(echo \$cur_$1_modify_time)
echo $fpm
1 Like

fpmurphy is correct. Your code worked perfectly.