我可以在bash中的文件路径中使用变量吗?如果是这样,怎么样?

The*_*pes 9 bash filepath

我正在尝试编写一个小的shell脚本来查找目录中最近添加的文件,然后将该文件移动到其他位置.如果我使用:

ls -t ~/directory | head -1
Run Code Online (Sandbox Code Playgroud)

然后将其存储在变量VARIABLE_NAME中,为什么我不能通过以下方式将其移动到〜/ otherdirectory:

mv ~/directory/$VARIABLE_NAME ~/otherdirectory
Run Code Online (Sandbox Code Playgroud)

我在这里搜索过谷歌搜索,但似乎没有关于在文件路径中使用变量的任何信息?有一个更好的方法吗?

编辑:这是脚本的一部分:

ls -t ~/downloads | head -1
read diags
mv ~/downloads/$diags ~/desktop/testfolder
Run Code Online (Sandbox Code Playgroud)

ass*_*aru 13

您可以在脚本中执行以下操作:

diags=$(ls -t ~/downloads | head -1)
mv ~/downloads/"$diags" ~/desktop/testfolder
Run Code Online (Sandbox Code Playgroud)

在这种情况下,diags赋值ls -t ~/downloads | head -1可以被调用mv.