Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parameter substitution bad substitution error on macOS High Sierra

Tags:

bash

macos

The ${parameter[^|^^|,|,,][pattern]} parameter substitution is giving me a bad substitution error.

$ echo $greeting
hello world
$ echo "${greeting^}."
-bash: ${greeting^}.: bad substitution

I updated to the latest bash version and keep getting the error.

GNU bash, version 4.4.19(1)-release (x86_64-apple-darwin17.3.0)

I've looked everywhere and the only suggestion I've found is making sure it's running bash 4.

$ echo $SHELL
/bin/bash

I'm running macOS High Sierra.

like image 573
Catalina De La Cuesta Avatar asked Mar 30 '18 11:03

Catalina De La Cuesta


1 Answers

Your default shell is not the bash shell (downloaded from brew install bash) that contains the v4 which supports the parameter expansion syntax you are referring to.

On macOS echo $BASH_VERSION will tell you the version of the current shell. bash --version tells you the version of the first bash in your $PATH. So the way you were looking at the version was not telling you the version that you were running.

You need to add the recent version of bash to the file /etc/shells as the last line and use the command to set the shell as the default on Terminal

chsh -s /usr/local/bin/bash "$USER"

After this close and re-open the Terminal to make it effect. Without adding this default option in your Terminal, you could only use the recent bash only on scripts with interpreter she-bang set to #!/usr/local/bin/bash

See also this Ask Different answer to - Update bash to version 4.0 on OSX

like image 145
Inian Avatar answered Sep 17 '22 11:09

Inian