5

What is the syntax error in this file? I can't spot it.

set-k8s-azure() { 
  export KUBECONFIG=~/.kube/config.azure-1 
}

set-k8s-minikube() { export KUBECONFIG=~/.kube/config.minikube }

minikube() {
  if [[ $@ == start* ]]; then
    set-k8s-minikube
  fi
  command minikube "$@"
}

alias pulr='if output=$(git status --porcelain) && [ -z "$output" ]; then git pull --rebase; else git stash save "pulr WIP saved" && git pull --rebase && git stash pop; fi'
alias vi=nvim

source ~/.bash_aliases produces:

bash: /home/niel/.bash_aliases: line 1: syntax error near unexpected token `('
bash: /home/niel/.bash_aliases: line 1: `set-k8s-azure() { '
  • 1
    The error should be what Kusalananda explains, but is that really the error message you see? I get syntax error: unexpected end of file (which is what I'd expect since the { } subshell is never closed because of the missing ;). What version of bash is this? – terdon Dec 14 '17 at 15:37
  • @terdon That is really it. "GNU bash, version 4.4.12(1)-release-(x86_64-pc-linux-gnu)" – Niel de Wet Dec 14 '17 at 15:44
  • OK, that's really weird. I'm using Arch and GNU bash, version 4.4.12(1)-release (x86_64-unknown-linux-gnu) and don't get the first error. – terdon Dec 14 '17 at 16:24
  • you'd get a syntax error on the ( if the line had whitespace in middle of the function name, like foo bar() { .... But they'd need to be something the shell recognizes as whitespace. I couldn't come up with how to recreate that with invisible characters; my Bash accepts e.g. zero-width non-joiners as parts of the function name. :D – ilkkachu Dec 14 '17 at 17:26

2 Answers2

11

I believe that the syntax error is here:

set-k8s-minikube() { export KUBECONFIG=~/.kube/config.minikube }

The {...} construct needs either a newline or a ; before the final }:

set-k8s-minikube() { export KUBECONFIG=~/.kube/config.minikube; }

Also, I'd advise that you use $HOME rather than ~ in scripts, partly because it serves as documentation and partly because $HOME behaves like a variable whereas ~ does not (see Why doesn't the tilde (~) expand inside double quotes?).

psmears
  • 465
  • 3
  • 8
Kusalananda
  • 333,661
  • Thanks, that's part of the problem. The other part, and apparently the one that is producing the syntax error I saw, is the user of the hyphen in the function name. Replacing those with underscores solves the problem. – Niel de Wet Dec 14 '17 at 15:47
  • set_k8s_minikube() { export KUBECONFIG=~/.kube/config.minikube; } works. – Niel de Wet Dec 14 '17 at 15:49
  • 1
    Hmm, function name with hyphens is OK for me on bash v4.3.48 – glenn jackman Dec 14 '17 at 15:50
  • @NieldeWet I have no old Bash to test with, unfortunately... – Kusalananda Dec 14 '17 at 15:54
  • Apparently hyphens are not guaranteed: https://unix.stackexchange.com/questions/168221/are-there-problems-with-hyphens-in-functions-aliases-and-executables, although the accepted answer there claims that bash does support it. – Niel de Wet Dec 14 '17 at 17:17
  • 1
    Even the Bash 3.2.57 on my Mac supports hyphens in function names. – ilkkachu Dec 14 '17 at 17:20
2

Dashes are not valid in function names. (For more details, see Are there problems with hyphens in functions, aliases, and executables?) Bash is normally tolerant of certain invalid chars, but in some cases it's not, and I have no idea why, though I've seen this before. So replace the dashes with underscores (and add a semicolon like Kusalananda said) and it'll be fine:

set_k8s_azure() { 
  export KUBECONFIG=~/.kube/config.azure-1 
}

set_k8s_minikube() { export KUBECONFIG=~/.kube/config.minikube; }
wjandrea
  • 658