Bash中带有参数的别名-Mac [英] Alias with Argument in Bash - Mac

查看:57
本文介绍了Bash中带有参数的别名-Mac的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,尝试在bash中为别名添加参数.我使用的是Mac.我搜索了一些相关问题: Bash别名具有自动完成功能的Bash中的别名,还有一些尚无法解决.我知道我需要使用一个函数来创建一个接受输入的别名,但目前尚不清楚它的含义是否类似于以下任何选项..bash_profile中的所有输入.

Hi there trying to add an argument to an alias in bash. I'm using a Mac. I've searched a number of related questions: Alias in Bash, Alias in Bash with autocomplete, and a few others yet still can't sort this out. I know I need to use a function to create an alias that takes an input but it's unclear if its meant to look like any of the below options. All inputs in .bash_profile.

function mins_ago () { `expr $(date +%s) - 60 \* "$1"`; }

alias mins_ago = function mins_ago () { `expr $(date +%s) - 60 \* "$1"`; }

alias mins_ago = "function mins_ago () { `expr $(date +%s) - 60 \* "$1"`; }"

alias mins_ago = function mins_ago () { `expr $(date +%s) - 60 \* $1`; }

这些似乎都不起作用.我不是语法错误,就是无法识别参数.

None of these seem to work. I either get a syntax error or it doesn't recognize the argument.

您要放入 .bash_profile 以便正确排序的实际行是什么?先感谢您.是否包含 alias 位?还是我只继续进行函数定义?

What is the actual line you'd put in .bash_profile to sort this properly? Thank you in advance. Is the alias bit included or do I just proceed to the function definition?

推荐答案

bash别名不支持参数,因此您需要使用bash函数,并使用 bash 算术运算符$(())

Bash aliases don't support arguments, so you need to use a bash function, and use the bash arithmetic operator $(())

function mins_ago() {
    printf "%s" "$(( $(date +%s) - (60 * $1) ))"
}

.bash_profile 中添加上述功能,然后在命令行中对其进行测试,

Add the above function in .bash_profile, and now testing it in the command-line,

date +%s
1485414114

value="$(mins_ago 3)"
printf "%s\n" "$value"
1485413834

(或)没有临时变量以转换为 GNU日期的可读格式,请执行

(or) without a temporary variable to convert to readable format in GNU date, do

printf "%s\n" "$(date -d@$(mins_ago 3))"

这篇关于Bash中带有参数的别名-Mac的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆