【问题标题】:bash: How to assign a directory name with spaces and single quotes to a variablebash:如何将带有空格和单引号的目录名称分配给变量
【发布时间】:2022-08-03 17:26:11
【问题描述】:

这是一个示例目录

drwxr-xr-x 1 vangryman 197121 0 Jul 29 13:27 \"2022-07-29 13.26.09 Very Angryman\'s Personal Meeting Room\"/

所以我正在尝试获取目录名称,以便稍后 cd 进入它

$ cat get_zoom.sh

#!/bin/bash

dir=$(ls -tr ../../Downloads/Documents/Zoom/ | tail -1)

cd \"\\\"\\\"$dir\\\"\\\"\"

pwd

但是我不喜欢我如何包装引号

$ sh -x get_zoom.sh

++ ls -tr ../../Downloads/Documents/Zoom/

++ tail -1

+ dir=\'2022-07-29 13.28.21 Very Angryman\'\'\\\'\'s Personal Meeting Room\'

+ cd \'\"\"2022-07-29 13.28.21 Very Angryman\'\\\'\'s Personal Meeting Room\"\"\'

get_zoom.sh: line 3: cd: \"\"2022-07-29 13.28.21 Very Angryman\'s Personal Meeting Room\"\": No such file or directory
  • 你为什么要标记这个重击, 当你运行它时?

标签: bash variables directory


【解决方案1】:

我没有看到分配本身有问题,除非您使用的是非常旧的 shell 版本,在这种情况下,您应该编写

dir="$(.....)"

但是您的 cd 命令很奇怪。应该很简单

cd "$dir"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-07
    • 2023-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多