ฉันจะเชื่อมสตริงและตัวแปรเข้าด้วยกันในเชลล์สคริปต์ได้อย่างไร?
stringOne = "foo"
stringTwo = "AnythingButBar"
stringThree = "? และ?"
ฉันต้องการที่จะแสดงผล "foo และอะไรก็ได้ ButBar"
ฉันจะเชื่อมสตริงและตัวแปรเข้าด้วยกันในเชลล์สคริปต์ได้อย่างไร?
stringOne = "foo"
stringTwo = "AnythingButBar"
stringThree = "? และ?"
ฉันต้องการที่จะแสดงผล "foo และอะไรก็ได้ ButBar"
คำตอบ:
ไม่มีอะไรพิเศษคุณเพียงแค่เพิ่มลงในการประกาศของคุณ
ตัวอย่างเช่น:
[Zypher@host01 monitor]$ stringOne="foo"
[Zypher@host01 monitor]$ stringTwo="anythingButBar"
[Zypher@host01 monitor]$ stringThree=$stringOne$stringTwo
[Zypher@host01 monitor]$ echo $stringThree 
fooanythingButBarหากคุณต้องการคำที่แท้จริง 'และ' ระหว่างพวกเขา:
[Zypher@host01 monitor]$ stringOne="foo"
[Zypher@host01 monitor]$ stringTwo="anythingButBar"
[Zypher@host01 monitor]$ stringThree="$stringOne and $stringTwo"
[Zypher@host01 monitor]$ echo $stringThree 
foo and anythingButBarecho ${stringOne}and${stringTwo}ถ้าคุณไม่ต้องการที่ว่าง
                    stringThree=$stringOne" and "$stringTwoนอกจากนี้คุณยังสามารถทำ
                    ถ้าคุณมี:
stringOne="foo"
stringTwo="anythingButBar"
stringThree="%s and %s"คุณสามารถทำได้:
$ printf "$stringThree\n" "$stringOne" "$stringTwo"
foo and anythingButBar
$ stringOne="foo"เช่น และไม่ควรปรากฏพรอมต์บนบรรทัดเอาต์พุต (บรรทัดหลัง echos) มิฉะนั้น +1