logo

Extra Block Types (EBT) - New Layout Builder experience❗

Extra Block Types (EBT) - styled, customizable block types: Slideshows, Tabs, Cards, Accordions and many others. Built-in settings for background, DOM Box, javascript plugins. Experience the future of layout building today.

Demo EBT modules Download EBT modules

❗Extra Paragraph Types (EPT) - New Paragraphs experience

Extra Paragraph Types (EPT) - analogical paragraph based set of modules.

Demo EPT modules Download EPT modules

Scroll

Drush 显示错误:支持 bash 使用 'source' 命令,如果当前不是在 bash 中运行,则回退使用 $0

02/09/2025, by Ivan

我有大量配置文件,通常使用以下命令导入配置,并通过 php -d memory_limit=-1 来禁用 PHP 的内存限制:

php -d memory_limit=-1 ./vendor/bin/drush config-import -y

但这次我遇到了一个错误:

# 支持 bash 使用 `source` 命令,如果当前不是在 bash 中运行,则回退使用 $0
# https://stackoverflow.com/a/35006505/6512
selfArg="$BASH_SOURCE"
if [ -z "$selfArg" ]; then
    selfArg="$0"
fi

self=$(realpath $selfArg 2> /dev/null)
if [ -z "$self" ]; then
    self="$selfArg"
fi

dir=$(cd "${self%[/\\]*}" > /dev/null; cd ../drush/drush && pwd)

if [ -d /proc/cygdrive ]; then
    case $(which php) in
        $(readlink -n /proc/cygdrive)/*)
            # 在 Cygwin 中使用 Windows php,需要转换路径
            dir=$(cygpath -m "$dir");
            ;;
    esac
fi

export COMPOSER_RUNTIME_BIN_DIR="$(cd "${self%[/\\]*}" > /dev/null; pwd)"

# 如果 bash 正在 source 此文件,则也要 source 目标文件
bashSource="$BASH_SOURCE"
if [ -n "$bashSource" ]; then
    if [ "$bashSource" != "$0" ]; then
        source "${dir}/drush" "$@"
        return
    fi
fi

"${dir}/drush" "$@"

你可以通过传递 --php-options 参数来避免这个错误:

./vendor/bin/drush --php-options='-d memory_limit=-1' config-import -y

为什么 php -d memory_limit=-1 vendor/bin/drush … 现在会报错?

当你通过 php 直接运行 Drush 文件时,其实是让 PHP 解释器去解析一个 bash 脚本:

php -d memory_limit=-1 ./vendor/bin/drush status

PHP 会尝试执行第一个非 PHP 的代码行(如 # Support bash …),并立刻中止执行,输出这个包装器脚本的源代码。这正是你遇到的问题。

这个变化是在 Drush 13.3 中引入的,相关讨论可见上游 issue:“Running drush as php script fails after updating from 13.2.0”。