创建别名来打印命令而不是执行它

创建别名来打印命令而不是执行它

我刚刚开始自定义我的 bash 配置文件,我想知道如何输入别名并在 CLI 上启动它而不实际执行它。以 Curl 为例,我想输入一个别名并得到类似的东西:

curl https://api.github.com/users/agituser

出现在 CLI 上,允许我编辑 URL。有点像 Autokey。

答案1

假设您有一个ONE包含 url 的变量https://api.github.com/users/agituser,您可以使用以下命令设置一个别名,editurl该别名将为您提供可编辑的输出:

alias editurl='read -e -i "$ONE" URL && curl $URL'

man read

NAME
       read - read from a file descriptor
  • -e 如果标准输入来自终端,则使用 Readline 获取行
  • -i text 用作TEXTReadline 的初始文本

所以editurl会给你,

ron@ron:~$ editurl 
https://api.github.com/users/agituser #this line is editable

您可以更改 URL(它将被保存在 `$URL 中)或者不更改。

如果不更改 url:

ron@ron:~$editurl 
https://api.github.com/users/agituser
{
  "login": "agituser",
  "id": 787606,
  "avatar_url": "https://avatars.githubusercontent.com/u/787606?v=3",
  "gravatar_id": "",
  "url": "https://api.github.com/users/agituser",
  "html_url": "https://github.com/agituser",
  "followers_url": "https://api.github.com/users/agituser/followers",
  "following_url": "https://api.github.com/users/agituser/following{/other_user}",
  "gists_url": "https://api.github.com/users/agituser/gists{/gist_id}",
  "starred_url": "https://api.github.com/users/agituser/starred{/owner}{/repo}",
  "subscriptions_url": "https://api.github.com/users/agituser/subscriptions",
  "organizations_url": "https://api.github.com/users/agituser/orgs",
  "repos_url": "https://api.github.com/users/agituser/repos",
  "events_url": "https://api.github.com/users/agituser/events{/privacy}",
  "received_events_url": "https://api.github.com/users/agituser/received_events",
  "type": "User",
  "site_admin": false,
  "public_repos": 0,
  "public_gists": 0,
  "followers": 0,
  "following": 0,
  "created_at": "2011-05-14T10:07:35Z",
  "updated_at": "2015-04-07T14:47:28Z"
}

或者更改网址:

ron@ron:~$editurl 
https://api.github.com/user
{
  "message": "Requires authentication",
  "documentation_url": "https://developer.github.com/v3"
}

相关内容