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() { '
4
  • 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? Commented Dec 14, 2017 at 15:37
  • @terdon That is really it. "GNU bash, version 4.4.12(1)-release-(x86_64-pc-linux-gnu)" Commented Dec 14, 2017 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. Commented Dec 14, 2017 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 Commented Dec 14, 2017 at 17:26

2 Answers 2

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?).

6
  • 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. Commented Dec 14, 2017 at 15:47
  • set_k8s_minikube() { export KUBECONFIG=~/.kube/config.minikube; } works. Commented Dec 14, 2017 at 15:49
  • 1
    Hmm, function name with hyphens is OK for me on bash v4.3.48 Commented Dec 14, 2017 at 15:50
  • @NieldeWet I have no old Bash to test with, unfortunately... Commented Dec 14, 2017 at 15:54
  • 1
    Even the Bash 3.2.57 on my Mac supports hyphens in function names. Commented Dec 14, 2017 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; }
2
  • Accepting this answer, for the sake of future readers, because it summarizes the whole problem. Commented Dec 15, 2017 at 6:38
  • @NieldeWet That's seem reasonable to me. Commented Dec 15, 2017 at 8:51

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.