1

I have the below command "${CHROMEDRIVER//@([\/])/}" is shell file and on running bash file.sh, it doesn't work:

#!/bin/bash

CHROMEDRIVER="75.0.3770.8/"
echo $CHROMEDRIVER
echo "${CHROMEDRIVER//@([\/])/}"

Screenshot showing unwanted slashes in output

But, when I run the same cmd "${CHROMEDRIVER//@([\/])/}" in terminal, it works great

Screenshot showing slashes removed on interactive invocation

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
Prashanth Sams
  • 19,677
  • 20
  • 102
  • 125

1 Answers1

2

This command requires extglob support to be enabled. Presumably you're turning it on (with shopt -s extglob) in your dotfiles, but it's off-by-default in scripts.

See the bash-hackers' wiki on extended pattern language, describing the specific items that require the extglob flag to be enabled; @(...) is among them.

Thus, to fix your script without changing the pattern you're replacing:

#!/usr/bin/env bash

shopt -s extglob

CHROMEDRIVER="75.0.3770.8/"
echo "$CHROMEDRIVER"
echo "${CHROMEDRIVER//@([\/])/}"

...though insofar as your goal is just to strip a trailing slash, use "${CHROMEDRIVER%/}" instead, as described in the bash-hackers' wiki page on parameter expansion.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441