0

I am trying to set up an alias in Powershell. For some weird reason, they do not support spaces (arguments), so I have to create a function instead

I ran this command:

function glo { git log --oneline }

I expected that afterwards I could run this using glo. But running glo results in glo : The term 'git log --oneline' is not recognized as the name of a cmdlet, function, script file, operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again instead of running git log --oneline

How to set up this alias? Why is it difficult to set up aliases in Powershell compared to Linux where this is easy with the .zshrc or .bashrc file.

1
  • 3
    Function as presented should work fine. Either show what you are actually running or provide more details. Which powershell version? Did you get some funny characters that PS does not treat as whitespace between the command and arguments? Commented May 14, 2023 at 19:57

1 Answer 1

1

Note: The function definition in your question is technically correct, but the error message suggests you used a different function definition - see the bottom section. The first section contains general information.

  • Unlike in POSIX-compatible shells such as bash, you cannot include pass-through arguments in a PowerShell alias definition - you indeed need a function for that.

  • PowerShell aliases - by design - are simply alternative names for other commands, with no ability to "bake in" arguments - for more information about aliases vs. function in PowerShell, see this answer.

Therefore:

# Defines a 'glo' command that executes `git log --oneline` with # additional arguments, if any, passed through. function glo { git log --oneline @args } 
  • Note the use of @args rather than $args, which is an instance of parameter splatting, which with an array variable passes the array's elements as individual arguments to the target command.

    • Since you're calling an external program (git), this isn't strictly necessary, because $args would do that too; if the target command were a PowerShell command, however, only @args would pass all arguments through individually.

    • Note:

      • The function also works if you pass no arguments, in which case git log --oneline is executed.

      • More effort is needed if your function also needs to support pipeline input - see this answer for an example.

  • For a more sophisticated way of wrapping commands, using proxy functions, see this answer.

  • To make such "aliases" (wrapper functions) available in all future sessions, add them to your $PROFILE file.


As for what you tried:

The function definition in your question - function glo { git log --oneline } - is actually correct, assuming you don't need support for additional, ad hoc pass-through arguments when calling glo (and assuming you don't need to support pipeline input).

However, the error message you're showing implies that you instead defined your function as follows:

# !! BROKEN function glo { & 'git log --oneline' } 

This cannot be expected to work, because &, the call operator, expects a command name or executable path only as its first argument - any arguments must be passed separately.

If you wanted to use & in the function definition - which isn't necessary, given that command name git neither has spaces nor is expressed in terms of variables - define the function body as:
& git log --oneline

Sign up to request clarification or add additional context in comments.

3 Comments

This does not change anything, it still tries to call the command "git log --oneline" as if it was one command. Furthermore relaying the arguments to the command the function calls is not the issue here nor needed.
@Riikka, it's fine if you don't need to relay arguments - it'll work either way. Paste the function definition ass shown into an interactive PowerShell session and then run glo
@Riikka, it was previously in a comment, but I've moved the explanation of what the problem with your actual function definition was, as implied by the error message, into a new section of the answer. (The function definition you're showing is actually correct, if you don't need pass-through arguments.)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.