如何在Bash中从变量中删除一些字母

huangapple go评论70阅读模式
英文:

How to remove some letters form a variable in bash

问题

我有这个URL

Https://github.com/RayaneQH/test.git

我想编写一个Bash脚本,它接受这个输入并只输出"test"(仓库名称)
注意:
如果有任何使用Git的方法来做到这一点,我更喜欢你给出两种解决方案,一种是Bash,一种是Git

我搜索了一下,但没有找到任何东西。

英文:

I have this Url

Https://github.com/RayaneQH/test.git

I want to write a bash script that take this input and output only the test (the repository)
Note:
If there is any way to do this using git , i prefer that you give the two solutions , one in bash and one in git

I google it and i didn't find anything

答案1

得分: 2

你可以使用你的 shell 中的参数扩展来删除最大的前缀模式:

url='https://github.com/RayaneQH/test.git'
url="${url##*/}"

这将删除直到最后一个斜杠(包括斜杠本身)的所有内容。

我不知道如何在 Git 中实现这个,因为 Git 是一个版本控制系统,而不是一个脚本语言。

英文:

You can use parameter expansion of your shell to remove the largest prefix pattern:

url='https://github.com/RayaneQH/test.git'
url="${url##*/}"

This will remove everything up to and including the final slash.

I don't know how this could be done with Git, since Git is a version control system and not a scripting language.

答案2

得分: 0

"使用git"

使用GitHub APIGitHub CLI

gh api 'repos/RayaneQH/test' --jq '.name'

如果仓库存在,将输出:

test

省略--jq选项以获取有关仓库的所有数据,以JSON格式。

英文:

"with git"

Using the github API with the github CLI

gh api 'repos/RayaneQH/test' --jq '.name'

would output (if the repo existed)

test

Omit the --jq option to get all data about the repo in JSON format.

huangapple
  • 本文由 发表于 2023年8月4日 02:29:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76830754.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定