Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash script: looping over arguments [duplicate]

Tags:

shell

unix

sed

I wrote a script (replace.sh) that replaces a ~ with ~\n. I was wondering how can I call this script with multiple arguments.

#!/bin/bash

for i in "$*"
 do
  sed 's/~/~\n/g' "$i"
done

For example I would like to call ./replace.sh text1 text2. It cannot read

the contents of text1 are: 1 ~ 1 ~ 1. After calling the script it should be

1 ~
1 ~
1 ~
like image 718
Paolo Avatar asked Aug 21 '13 21:08

Paolo


1 Answers

Use "$@" in your for loop:

for i in "$@"
do
   sed -i.bak 's/~/~\n/g' "$i"
done

Though I am not really sure if sed is doing what you've described here.

like image 70
anubhava Avatar answered Sep 22 '22 22:09

anubhava