0

In the following script

#!/bin/bash
shopt -s globstar
set -e

fwd_slsh='/'
space=' '
ampersands='&&'
bang='!'
opnbrc='['
clsbrc=']'
caret='^'

prefix=${bang}${fwd_slsh}${opnbrc}${caret}
suffix=${clsbrc}${fwd_slsh}

echo $prefix
echo $suffix

the resultant output is as shown below:

!/^
/

The resulting echo output omits the '[' and the ']'.

What is the change required to the above script so that both '[' and ']' are included in the echo outputs? Just wanted to reiterate that I would want to retain the above convention if possible, that is, include both the '[' and ']' as part of the variables as shown above.

My intention is to use the variables prefix and suffix in awk commands, along with other variables input by the user during runtime.

Appreciate your thoughts.

TIA

Vinod

Vinod
  • 155
  • 4
  • Odd, works as required for me. What version of bash? – Jaromanda X May 17 '23 at 04:25
  • I get GNU bash, version 4.4.19(1)-release (x86_64-redhat-linux-gnu) when I run bash --version – Vinod May 17 '23 at 04:28
  • oh, maybe something changed in bash between that and 5.1.4 - have you tried `prefix="${bang}${fwd_slsh}${opnbrc}${caret}"` - i.e. add quotes? – Jaromanda X May 17 '23 at 04:36
  • 1
    try `opnbrc='\['` – jsotola May 17 '23 at 04:49
  • 3
    I can't reproduce with the exact same version. Try running `bash -x ./that-script` which might give you a clue. In any case you forgot the quotes around the expansions in `echo $prefix` and `echo $suffix` and remember `echo` can't be used to output arbitrary data. More on that at [When is double-quoting necessary?](https://unix.stackexchange.com/q/68694) and [Why is printf better than echo?](https://unix.stackexchange.com/q/65803) – Stéphane Chazelas May 17 '23 at 04:59
  • 1
    How exactly are you executing the code that you show? – Kusalananda May 17 '23 at 06:43
  • I have tried all the suggestions but none of them seem to work, including printf. I am just not able to see the '[' or ']' in the output. – Vinod May 17 '23 at 21:02

0 Answers0