0

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.

Pranjal Doshi
  • 862
  • 11
  • 29

1 Answers1

2

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

KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • Thanks for the answer. This will work for me. I was wondering is there any way we can optimize this code. find and replace similar to python. `$*.replace('--native', '')`. – Pranjal Doshi Jul 14 '22 at 08:36
  • The code should be quite fast - it's only bash with no subprocesses. I do not think there's anything to optimize. – KamilCuk Jul 14 '22 at 08:36
  • ok Thanks. By optimize I mean we can reduce number of line. Though this works as expected :). – Pranjal Doshi Jul 14 '22 at 08:37
  • You can put the whole script on one single line, if you wish (separate commands with `;` or `&&`). – Renaud Pacalet Jul 14 '22 at 08:38