创建参数未按预期工作的bash别名和函数

创建参数未按预期工作的bash别名和函数,bash,curl,Bash,Curl,我有一个接受3个参数的脚本,它可以工作 #!/bin/bash curl --header "Authorization: token $GITHUB_CLI_TOKEN" \ --header "Accept: application/vnd.github.v3.raw" \ --remote-name \ --location "https://ap

我有一个接受3个参数的脚本,它可以工作

#!/bin/bash
curl --header "Authorization: token $GITHUB_CLI_TOKEN" \
                        --header "Accept: application/vnd.github.v3.raw" \
                        --remote-name \
                        --location "https://api.github.com/repos/$1/$2/contents/$3"
例如,使用有效的Github访问令牌

$ GITHUB_CLI_TOKEN=07D5E834792BF0B35C0DD9B1E18CC4CDB75E20E9 ./fetch.py miguelgrinberg Flask-SocketIO example/app.py
当我把它写成别名时,它不起作用

alias github-fetch-file='curl --header "Authorization: token $GITHUB_CLI_TOKEN" \
                         --header "Accept: application/vnd.github.v3.raw" \
                         --remote-name \
                         --location "https://api.github.com/repos/$1/$2/contents/$3"'
它也不是一个函数

function github-fetch-file() {
  curl --header "Authorization: token $GITHUB_CLI_TOKEN" \
       --header "Accept: application/vnd.github.v3.raw" \
       --remote-name \
       --location "https://api.github.com/repos/$1/$2/contents/$3"
}
错误
alias不起作用,因为行尾前的反斜杠是文字(因为单引号),并且没有参数扩展
$1
等。也是文字,函数应该作为脚本使用
GITHUB\u CLI\u TOKEN
可以在调用函数as for script之前进行内联设置。

当定义别名时,位置参数会展开,而不是在调用别名时展开,这是别名解决方案无法工作的原因。你的功能看起来很好,它应该可以工作。。。如果在调用它之前定义了
GITHUB\u CLI\u TOKEN
。是你干的吗?顺便问一下,为什么要命名bash脚本
fetch.py
?您确定要向我们显示所有内容吗?根据错误,请尝试在
远程名称中提供文件名
,或者如果您不希望保存输出,请将其删除。另外,检查主机是否在
hosts
文件中定义。
$ github-fetch-file miguelgrinberg Flask-SocketIO example/app.py
curl: Remote file name has no length!
curl: try 'curl --help' or 'curl --manual' for more information
curl: (6) Could not resolve host: miguelgrinberg
curl: (6) Could not resolve host: Flask-SocketIO
curl: (6) Could not resolve host: example