Home > Software design >  Find and replace string in command line args in bash
Find and replace string in command line args in bash

Time:07-14

I have bash script which takes set of command line argument. Now I want to remove some of the argument that I received from user and pass it to other script. How can I achieve this.

remove_arts.sh

echo "Prints all args passed $*"
# Add logic to remove args value --native/-nv/--cross
my_app_args=$(command to store result in $*)
bash run_my_app.sh my_app_args

So when I run that script with

bash remove_arts.sh --native -nv --execute_rythm

After processing bash run_my_app.sh --execute_rythm

I have checked Replace one substring for another string in shell script which suggested to use

first=${first//Suzy/$second}

But I am not able to use $second as empty string.

CodePudding user response:

For each argument, store all arguments that are not in the list of excludes inside an array. Then pass that array to your script.

# Add logic to remove args value --native/-nv/--cross
args=()
for i in "$@"; do
  case "$i" in
  --native|-nv|--cross) ;;
  *) args =("$i"); ;;
  esac
done
run_my_app.sh "${args[@]}"

You might want to research bash arrays and read https://mywiki.wooledge.org/BashFAQ/050 . Remember to check your scripts with shellcheck

  •  Tags:  
  • bash
  • Related