0

I am trying to set an alias to show value of specific date command. When I set the alias, I get correct value, but also get "command not found..." Is there a way to set this without the error?

Summary:

I am trying to get last day of previous month for the current day. Todays date is: 20210221

I am using the following command to get the date I want:

  
date -d "$(date +%Y-%m-01) -1 day" +%Y%m%d
20210131

Results:

I set alias for this in .bashrc

[root@centos_8_1 ~]# alias | grep date
alias test_date='$(date -d "$(date +%Y-%m-01) -1 day" +%Y%m%d)'

When I try to call, I get the correct value, but also get "command not found...

[root@centos_8_1 ~]# test_date
bash: 20210131: command not found...

Questions:

Am I missing something with syntax?

How can I set an alias for this date?

What is the command that is not found?

First time poster, thanks for help.

1 Answer 1

1

An alias is simply a way to replace some string at the start of a command with some other arbitrary string. What's happening in your case is that test_date is replaced with $(date -d "$(date +%Y-%m-01) -1 day" +%Y%m%d), which first runs a date command and then tries to run the resulting command substitution, resulting in "executing" the date string.

The solution is to simply remove the $() surrounding the alias string.

1
  • Thanks for the quick answer. I removed the first parentheses and it worked.
    – coyoteee3
    Commented Feb 22, 2021 at 4:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .