Shell variable interpolation

This is probably a very simple question, but for some reason I seem to look obvious.

I have a variable called filepath=/tmp/name

To access the variable, I know that I can do this $filepath

In my shell script, I tried to do something like this (reverse ticks are intended)

 `tail -1 $filepath_newstap.sh` 

This line fails, spirit! because the variable is not called $filepath_newstap.sh

How to add _newstap.sh to a variable name? Please note that reverse ticks are designed to evaluate the expression

+64
unix bash shell
Jul 12 '13 at 18:56
source share
3 answers

Using

 "$filepath"_newstap.sh 

or

 ${filepath}_newstap.sh 

or

 $filepath\_newstap.sh 

_ is a valid character in identifiers. There is no dot, so the shell tried to interpolate $filepath_newstap .

You can use set -u to make shell output with an error when you reference an undefined variable.

+126
Jul 12 '13 at 18:59
source share

Use curly braces around the variable name:

 `tail -1 ${filepath}_newstap.sh` 
+10
Jul 12 '13 at 18:58
source share

In bash : tail -1 ${filepath}_newstap.sh

+3
Jul 12 '13 at 19:01
source share



All Articles