Freeman
Freeman

Reputation: 12758

How to put special characters in variable and use them in string?

I'm wondering is there anyway to use for example , or ^ or % and so on, from variables in Bash ?

in instance I have three variables

var1='hello world'
var2=${var1:3}
var3='^'

I want to do this in bash ! please attention to my question I know it's very simple in other ways but how about this ?

echo ${var1:0:3}${var2$var3} # instead of echo ${var1:0:3}${var2^}

and finally output is :

heLlo world

Upvotes: 0

Views: 78

Answers (1)

dash-o
dash-o

Reputation: 14491

In theory, eval can do execute arbitrary code, but has many security issues, so it should be used as last resort. Use it only when you trust the input 100%.

var1='hello world'
var2=${var1:3}
var3='^'

eval echo '${var1:0:3}${var2'$var3'}'

Upvotes: 1

Related Questions