Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Find and replace string in command line args in bash

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading