shell脚本从其路径中查找文件名

ayu*_*ush 18 shell

您好,我想要一个简单的shell脚本,从文件的给定路径中查找文件的名称.喜欢

$path = "/var/www/html/test.php";
Run Code Online (Sandbox Code Playgroud)

那么我想在一些变量中获得价值"测试".也只有.php文件存在.我正在使用bash shell.谢谢

Aar*_*lla 45

尝试:

path="/var/www/html/test.php"
name=$(basename "$path" ".php")
echo "$name"
Run Code Online (Sandbox Code Playgroud)

引号仅用于防止$path包含空格时出现问题.


Hog*_*ill 18

使用内置的UNIX命令:

basename "/var/www/html/test.php"
Run Code Online (Sandbox Code Playgroud)