The accepted answer to Transform an array into arguments of a command? uses the following Bash command:

command "${my_array[@]/#/-}" "$1"

I'm trying to figure out what the /#/- part does, exactly. Unfortunately, I don't know what to call it, so I'm having trouble finding any documentation. I've looked through the Bash man page section on arrays and a few websites, but can't find anything.

2 Answers 2


This is an instance of pattern replacement in shell parameter expansion: ${parameter/pattern/replacement} expands ${parameter}, replacing the first instance of pattern with replacement. In the context of a pattern of this kind, # is special: it anchors the pattern to the start of the parameter. The end result of all this is to expand all the values in the my_array array, prepending - to each one (by replacing the empty pattern at the start of each parameter).


Yes, it is a pattern replacement in shell parameter expansion as:


But if the first character after the first / is either / or # or % it has the special meaning of all (repeated), start and end.


$ str='one_#two_two_three_one'

A single / will replace the first instance. The first instance of one:

$ echo "${str/one/x-x}"

Or the first instance of two:

$ echo "${str/two/x-x}"

The instance of one at the end:

$ echo "${str/%one/x-x}"

All repetitions of two:

$ echo "${str//two/x-x}"

The instance of one at the start:

$ echo "${str/#one/x-x}"

An string that start with # (quote the #):

$ echo "${str/\#two/x-x}"

But if you leave the # (unquoted) alone, the replacement is set at the beginning of the variable:

$ echo "${str/#/====}"

Furthermore, if the parameter is an array, the replacement is done on all elements:

$ str=( one two three )
$ echo "${str[@]/#/==}"
==one ==two ==three
  • The wording is a bit misleading as the # and % are part of the pattern while // is a different operator from / and use the same patterns. You can have pattern='#x'; echo "${var/$pattern}" (or ${var//$pattern}), but pattern=/x; echo "${var/$pattern}" is not the same as echo "${var//x}". Note that # and % can be combined in zsh, but not bash nor ksh. Oct 6, 2018 at 22:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.