0

I am trying to create paths using variables as follows:

set var=foo
set path="/home/user/prefix_$var_suffix/bar"

What is the proper way to embed the variable so that it doesn't keep reading past the _. As a bonus, is there a syntax that will work for perl and csh (or bash at the least)?

Stuart
  • 1,733
  • 4
  • 21
  • 34

1 Answers1

1

Use curly braces to delimit the variable. This works in csh and all Bourne-style shells (including bash). It also works in Perl.

set path="/home/user/prefix_${var}_suffix/bar"

Another way is to end the quotes:

set path="/home/user/prefix_""$var""_suffix/bar"

In Perl you would have to use the . concatenation operator:

$path = "/home/user/prefix_".$var."_suffix/bar"

Note that you shouldn't use path as a variable name in your csh script, as that's used for the search path to find executables.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Ah, sorry, meant Perl not Python. Can I have the opening brace outside the `$` if I want (for editor purposes)? – Stuart Jul 23 '13 at 03:51
  • It also works in Perl. No, you can't wrap the braces around the variable (that's how PHP does it, but not shells or Perl). – Barmar Jul 23 '13 at 03:53
  • You might want to add that in Perl you can also escape with forward slash: `$path = "/home/user/prefix_$var\_suffix/bar"` but I don't see a syntax to escape in both Perl and csh. – Stuart Jul 24 '13 at 00:29
  • @Stuart Minor nit, that's a backslash. – Barmar Jul 24 '13 at 06:06