0

I am sure 200% something like this has been asked but by google skills are eluding me and I need a solid example as I am getting all sorts of errors perhaps on escaping the ?. I thought I could do something like

${var1|?*|} 

as I want to take

asldkjljewrewr?adflksjfdljksdf

and strip it down to

asldkjljewrewr

using bash script.

Dean Hiller
  • 19,235
  • 25
  • 129
  • 212
  • 1
    `var='asldkjljewrewr?adflksjfdljksdf'; echo "${var%\?*}"` The `?` needs escaping since it is a special character to the shell, which means a single string like a dot `.` in regex – Jetchisel Jun 16 '20 at 20:46
  • 1
    `echo "${var1%%\?*}"` should work – anubhava Jun 16 '20 at 20:47
  • maybe spliting the variable woulb de an option. https://stackoverflow.com/questions/918886/how-do-i-split-a-string-on-a-delimiter-in-bash –  Jun 16 '20 at 20:48

2 Answers2

1

I don't know if it is exactly what you were looking for, but one could do it using the cut command:

#!/bin/bash

var1="asldkjljewrewr?adflksjfdljksdf"
var2="$(cut -d "?" -f1 <<< "$var1")"
echo "$var2"
# asldkjljewrewr
panadestein
  • 1,241
  • 10
  • 21
1

Looks like you were trying to use ${parameter%%pattern}:

var1=asldkjljewrewr?adflksjfdljksdf
echo=${var1%%\?*}

Notice the need for escaping the ? - otherwise it will be considered as "any character" in the glob.

You can find all supported forms of parameter substitution in the Parameter Expansion section of the Advanced Bash-Scripting Guide.

Leonardo Dagnino
  • 2,914
  • 7
  • 28