如何连接两个字符串来构建一个完整的路径 [英] How to concatenate two strings to build a complete path
问题描述
我试图写一个bash脚本。在这个脚本我想用户输入目录的路径。然后我想一些字符串在该字符串末尾追加和建立的一些子目录的路径。
例如,假设用户输入的字符串是这样的:
I am trying to write a bash script. In this script I want user to enter a path of a directory. Then I want to append some strings at the end of this string and build a path to some subdirectories. For example assume user enters an string like this:
/home/user1/MyFolder
现在我想有此目录中创建2个子目录,并复制一些文件。
Now I want to create 2 subdirectories in this directory and copy some files there.
/home/user1/MyFolder/subFold1
/home/user1/MyFolder/subFold2
我怎样才能做到这一点?
How can I do this?
推荐答案
这多个 /
被视为一个单一的 /
// ///目录子目录////文件
相同 /目录/子目录/文件
。
The POSIX standard mandates that multiple /
are treated as a single /
in a file name. Thus
//dir///subdir////file
is the same as /dir/subdir/file
.
这样一个串联两个字符串构建一个完整的路径是一个简单的:
As such concatenating a two strings to build a complete path is a simple as:
full_path="$part1/$part2"
这篇关于如何连接两个字符串来构建一个完整的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!