2

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?

2
  • For starters, you're using two different style quotes in your list definition, which tend to mean very different things. Commented Oct 10, 2011 at 2:04
  • 1
    For future reference, including the error message in your post is more useful to those trying to help than, "I got error". Commented Oct 10, 2011 at 7:56

3 Answers 3

4

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
Sign up to request clarification or add additional context in comments.

Comments

2

Try this:

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

Comments

2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.