user788171
user788171

Reputation: 17553

bash: variables and quotes in quotes

I would like to set a variable in bash called test_var Basically, I want echo test_var to output:

%let output="file_20120601.csv";

where 20120601 is a variable. I am trying to do this by using:

test_var='%let output="file_$1.csv";'
echo test_var

this doesn't work because $1 is not interpreted as variable, but interpreted as literally $1

Does anybody know how I can modify this to get it to do what I want it to do?

Upvotes: 1

Views: 552

Answers (2)

Nahuel Fouilleul
Nahuel Fouilleul

Reputation: 19315

this works too

test_var='%let output="file_'"$1"'.csv";'

because variables are not expanded between single quotes. and we can concatenate strings between single quotes and double quotes just writing strings beside.

Upvotes: 0

Lev Levitsky
Lev Levitsky

Reputation: 65791

It doesn't work because of single quotes. They make everything literal.

$ var='123'
$ foo="\"%hi there file_$var\""

$ echo $foo
"%hi there file_123"

Upvotes: 5

Related Questions