【问题标题】:Quoting a path in linux引用linux中的路径
【发布时间】:2020-04-29 18:36:55
【问题描述】:

我正在学习 shell 脚本并创建了一个脚本。在脚本中,我已将 find 命令输出分配给代码中的变量。但是审阅者评论说“路径需要引用,因为它们包含需要扩展的变量”。我没有得到这个。

MAIN_PROFILE="$(find /Users/$CURRENT_USER/Library/Group\ Containers/*.Office -type d -name "Main Profile")"

【问题讨论】:

标签: bash


【解决方案1】:

你可以这样引用$CURRENT_USER

MAIN_PROFILE="$(find /Users/"$CURRENT_USER"/Library/Group\ Containers/*.Office -type d -name "Main Profile")"

您需要这样做以防止分词或通配(参见https://github.com/koalaman/shellcheck/wiki/SC2086

例如使用这样的脚本:

#!/bin/bash
USER="jean claude"
find /home/"$USER"/ -type d -name "test"

它将完美运行。

但是如果你取消引用它:

find: ‘/home/jean’: No such file or directory
find: ‘claude/’: No such file or directory

您也可以引用审阅者所说的完整路径,这样您就不必转义空格:

MAIN_PROFILE="$(find "/Users/$CURRENT_USER/Library/Group Containers/"*.Office -type d -name "Main Profile")"

【讨论】:

    猜你喜欢
    • 2021-02-27
    • 2017-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-08
    • 2020-05-18
    • 1970-01-01
    相关资源
    最近更新 更多