-1

When I'm working with Kubernetes, I want to run commands that depend on my active branch. For that reason, having aliases would help me with other aliases that automatically run commands, given the current branch.

I'm trying to store the name of the current active local branch into a bash alias using a function, so that I can run other scripts without worrying about specifying the active branch, but I keep encountering an error on this.

function branch ()
{
    local result='git branch | grep ^\* | cut -c 3-';
    echo "$result"
}

alias get_branch=$(branch)

But when I try to run this, I get:

    usage: git [--version] [--help] [-C <path>] [-c <name>=<value>]
               [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
               [-p | --paginate | -P | --no-pager] [--no-replace-objects] [--bare]
               [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
               <command> [<args>

.... (Same output as just entering 'git')

(cut -c 3- removes the * and space following it when active git branch gets listed) e.g.

* feature/ch20372 ch20372 ch12345

The strange part is both of these work:

alias IMLAZY='git branch |grep \* | cut -d " " -f2'
alias TEST='git branch | grep ^\* | cut -c 3-'

Which makes me think

  1. Could be syntax issues with my ~/.bash_aliases elsewhere?

  2. Issue with ZSH somehow?

  3. Syntax error somewhere in the function definition?

Dave Liu
  • 906
  • 1
  • 11
  • 31
  • I was trying to follow: https://www.linuxjournal.com/content/return-values-bash-functions – Dave Liu Jul 23 '19 at 18:46
  • @EncryptedWatermelon No... this is only tangentially relevant to that question. Sure, I'm using a similar command, but I want to know how to save this in a bash alias from a function. – Dave Liu Jul 23 '19 at 18:52
  • 1
    The error doesn't seem related to the code you're showing. – Benjamin W. Jul 23 '19 at 18:52
  • What should happen if you enter `get_branch`? – Cyrus Jul 23 '19 at 18:53
  • I just tried running your code and it worked properly. How are you running this snippet of code? The error you are getting and the code do not seem to be related. – Jason K Lai Jul 23 '19 at 18:54
  • https://stackoverflow.com/search?q=%5Bbash%5D+%5Bgit%5D+current+branch+variable – phd Jul 23 '19 at 18:58
  • @Cyrus the result should print the current working branch without * or space. – Dave Liu Jul 23 '19 at 18:59
  • @Benjamin W. It seems that error was syntax related, but it seems like there's still an error; I've updated the question. – Dave Liu Jul 23 '19 at 18:59
  • The question, at least, is using single quotes rather than backquotes (or preferably `$(...)`). As written, you aren't going to get the usage message you claim, because nothing will run `git`. – chepner Jul 23 '19 at 19:19

2 Answers2

5

Could be simpler to use the appropriate plumbing command : git rev-parse

(Named here gitcb for current branch, can be arbitrary)

alias gitcb='git rev-parse --abbrev-ref HEAD'
Dave Liu
  • 906
  • 1
  • 11
  • 31
Romain Valeri
  • 19,645
  • 3
  • 36
  • 61
  • 1
    I didn't downvote, but OP specifically asked for bash in title. – EncryptedWatermelon Jul 23 '19 at 19:16
  • @EncryptedWatermelon Yes, I've wondered if it was the reason of the downvote. But it was unsure that OP actually needed a bash alias. I figured it could have been a bad assumption on his part and waited for clarification. But you might very well be right nonetheless. Anyway, both our solutions work, I guess he'll be fine either way :-) – Romain Valeri Jul 23 '19 at 19:18
  • Yeah, needs to be a bash alias, clarified reasoning/purpose in the question – Dave Liu Jul 23 '19 at 19:20
  • 1
    @Dave Liu thanks for clarification. Editing. – Romain Valeri Jul 23 '19 at 19:21
  • @DaveLiu Why are you writing a function at all to generate the body of the alias, and why do you "need" an alias in the first place? – chepner Jul 23 '19 at 19:21
  • @chepner because I don't know how else I would do this. Usually, I use functions to generate the body of the alias in order to automate certain parameters that I don't have to specify. When I'm working with Kubernetes, I want to run commands that depend on my active branch. For that reason, having aliases would help me with other aliases that automatically run commands, given the current branch. – Dave Liu Jul 23 '19 at 19:23
  • 1
    I challenge you to find a situation where `git cb` (as defined in the previous version of this answer) would fail where a `bash` alias (that has to call `git` anyway) would succeed. – chepner Jul 23 '19 at 19:38
  • @chepner string length – Dave Liu Jul 23 '19 at 21:36
  • 1
    You might want to elaborate on that. – chepner Jul 23 '19 at 21:46
  • I admit I share chepner's doubts. Are we missing something in the picture? – Romain Valeri Jul 23 '19 at 21:47
  • @chepner The point is, I want a variable that I can insert somewhere else. Sure, technically if I assign this to an alias, it works. But I would need to do an additional step to evaluate the definition. I already knew 3+ ways to get the current branch name, before making this question. I'm trying to figure out: 1) Why MY code isn't working. 2) How to incorporate this into other functions/aliases 3) How to store the result of a function in a variable Yes, `alias=` is a pedantic, small additional step, but I took off my downvote as soon as @RomainValeri amended his answer. – Dave Liu Jul 23 '19 at 21:50
  • I thought I might need to do something like `alias asdf=$(git cb)`, but that didn't work for me. That additional step might seem trivial, but it wasn't. – Dave Liu Jul 23 '19 at 21:56
0
alias IMLAZY='git branch |grep \* | cut -d " " -f2'

:)

EncryptedWatermelon
  • 4,788
  • 1
  • 12
  • 28
  • I think I might have had syntax errors in my bash session that carried over, when I tried to re-write things. It seems I was defining the function incorrectly somehow, but still not sure what I did wrong. I'm still confused why this and `alias git_branch='git branch | grep ^\* | cut -c 3-'` weren't working before. – Dave Liu Jul 23 '19 at 19:09
  • Not sure. `alias git_branch='git branch | grep ^\* | cut -c 3-'` worked for me – EncryptedWatermelon Jul 23 '19 at 19:15
  • `git branch | grep '*'` is an anti-pattern. Get the branch name with `git rev-parse --abbrev-ref HEAD` – William Pursell Jul 23 '19 at 19:34