Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String replacement in bash - bad substitution error

Tags:

linux

bash

I newbie in bash scripting but i don't uderstand why it's not work

#!/bin/bash
foo=foobarfoobar
echo ${foo//bar/baz}

bad substitution error on line 3

like image 318
Aristarhys Avatar asked Jan 22 '12 11:01

Aristarhys


1 Answers

That substitution works fine in Bash 4.2.8 (and looks fine according to the documentation).

My best guess would be that you're not actually using Bash - how are you invoking the script? If you're doing sh script.sh you may well be running it with Dash or something similar (and Dash does indeed give a substitution error on line 3). Try explicitly running it with Bash (bash script.sh).

If it turns out you are actually using Dash, there's some useful information on the differences and how to go back to using Bash (if you want to) here: https://wiki.ubuntu.com/DashAsBinSh

like image 111
Chris Avatar answered Oct 20 '22 17:10

Chris