Ahmad
Ahmad

Reputation: 9658

How to remove a substring from string in SH not bash

I would like to modify input parameters of an SH script (it begins with #!/bin/sh). I found some solutions but they don't work here and need bash. They give me bad substitution error. so I look for a solution that works in SH (or whatever it is called)

The bash_params could be like "_learn _vil=bar _meet=foo". Here "_learn" acts as a flag. I want to set some variabels based on this flag and then remove it so that I can set other variables with eval.

Also, if you know better approaches please let me know

case $bash_params in
  *"_learn"*) # learn is enabled
      _learn_sp=True
      tt="_learn"
      bash_params="${bash_params%"$tt"}" # it doesn't work
      bash_params="${bash_params/_learn//}" # this gives Bad substitution error

      _lsp=False
    ;;

eval ${bash_params}

Upvotes: 0

Views: 125

Answers (1)

Barmar
Barmar

Reputation: 780724

Pipe to sed.

bash_params=$(echo "$bash_params" | sed "s/$tt//")

Upvotes: 2

Related Questions