0

What can I do to make this alias working good?

alias mcd="echo Go to  $1 ; cd $1"

When I write alias mcd="echo Go to $1" - than Go to work normally. If I add cd $1 the alias works, but doesn't print the name of the directory, value of $1="" for first part of alias. How to solve this?

Separately both parts os alias work, but together - no.

I read this, but it's 8 years old. Is it possible to do this without using a function?

6
  • 3
    Functions are the right tool for this job. Aliases are the wrong one. Do you have a practical reason for the constraint? Commented Oct 2, 2023 at 13:31
  • 1
    (to be explicit: no, nothing has changed that invalidates the linked question's answers over the last decade; aliases being considered bad practice goes back a lot further than that, too). Commented Oct 2, 2023 at 13:32
  • 1
    BTW, you can see the history of the bash IRC channel's factoid database on the topic at wooledge.org/~greybot/meta/alias; as you can see, some variant on "If you have to ask, you probably want to use a function instead" has always been the standard advice. Commented Oct 2, 2023 at 13:35
  • 3
    Aliases in bash don't take parameters. All the parameters are appended to the expanded alias, for anything more complex you need a function. Commented Oct 2, 2023 at 13:36
  • Now, if there's a practical constraint you need a workaround for, we can try to build that workaround for you -- but we need to know exactly why functions are impractical to use in your current scenario: if we don't know the letter of the rules you're being evaluated against, we don't know which mechanics will work to skirt them. (For example, one can embed a function within an alias, or have an alias start a whole new shell interpreter). Whereas if it's just a matter of curiosity, that's off topic here; see the "practical" criteria in stackoverflow.com/help/dont-ask Commented Oct 2, 2023 at 13:45

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.