How connect variable with a string in Bash?
I am writi开发者_运维百科ng 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?
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
Try this:
list='/home/ea/students'
...
cat "${list}/admin.txt"
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
精彩评论