Elham abbasi
Elham abbasi

Reputation: 439

How connect variable with a string in Bash?

I am writing a bash script , I used 1 variable in my bash file like below

list=`/home/ea/students'

I wrote below link in my bash but I got error

cat $list /admin.txt

Do you know how can I connect variable and string together?

Upvotes: 2

Views: 6743

Answers (3)

Dimitre Radoulov
Dimitre Radoulov

Reputation: 28000

You can go with either:

cat "$list/admin.txt"

In this case the braces '{}' are not mandatory as the / is not a valid identifier name character.

... or, if you need a variable, recent bash versions provide more concise way for appending:

bash-4.1$ list=/home/ea/students
bash-4.1$ list+=/admin.txt
bash-4.1$ printf '%s\n' "$list"
/home/ea/students/admin.txt

Upvotes: 2

johnsyweb
johnsyweb

Reputation: 141790

Firstly you need to use single quotes (''') around strings, not backticks ('`')

list='/home/ea/students'

To append a string to a variable, do the following:

list=${list}/admin.txt

Demo:

echo $list
/home/ea/students/admin.txt

Upvotes: 4

imm
imm

Reputation: 5919

Try this:

list='/home/ea/students'
...
cat "${list}/admin.txt"

Upvotes: 2

Related Questions