0

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.

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?

Inian
  • 80,270
  • 14
  • 142
  • 161
HectorOfTroy407
  • 1,737
  • 5
  • 21
  • 31
  • Stack Overflow is a site for programming and development questions. This question appears to be off-topic because it is not about programming or development. See [What topics can I ask about here](http://stackoverflow.com/help/on-topic) in the Help Center. Perhaps [Super User](http://superuser.com/) or [Unix & Linux Stack Exchange](http://unix.stackexchange.com/) would be a better place to ask. Also see [Where do I post questions about Dev Ops?](http://meta.stackexchange.com/q/134306) – jww Jan 26 '17 at 08:05

4 Answers4

2

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) ))"
}

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

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

printf "%s\n" "$(date -d@$(mins_ago 3))"
Rob Bednark
  • 25,981
  • 23
  • 80
  • 125
Inian
  • 80,270
  • 14
  • 142
  • 161
1

add this to your .bashrc and source it

mins_ago() {
  if [[ $@ != "" ]]; then
    command expr $(date +%s) - 60 \* "$@"
  else
    command echo "command error: mins_ago <integer>"
  fi
}

output:

$ mins_ago 1
1485414404
$ mins_ago
command error: mins_ago <integer>
sa77
  • 3,563
  • 3
  • 24
  • 37
0

Just type

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

It works for GNU bash 4.1.2:

$ mins_ago 5
bash: 1485411776: command not found

To avoid the error, use echo:

mins_ago () {
 echo `expr $(date +%s) - 60 \* "$1"` 
}
Djura
  • 13
  • 4
  • Rather than adding `echo`, remove the backquotes. – Gordon Davisson Jan 26 '17 at 06:42
  • `-bash: /Users/CloudFlare/.bash_profile: line 13: syntax error near unexpected token `{`expr $(date +%s) - 60 \* $1`' -bash: /Users/CloudFlare/.bash_profile: line 13: `function _hours(){`expr $(date +%s) - 60 \* $1`;} _hours '` when i try `mins_ago () { expr $(date +%s - 60 \* "$1"; }` – HectorOfTroy407 Jan 26 '17 at 06:51
  • @HectorOfTroy407: Can you try my answer above? – Inian Jan 26 '17 at 06:54
0

Remove the backticks from your first try, and you should be fine.

The function will then output the timestamp, and you can write echo "$(mins_ago)".

Roland Illig
  • 40,703
  • 10
  • 88
  • 121