'sed' with Positional Parameters

I'm new with 'sed' and for sure something still I don't understand yet with it. If you see my output on ">Output..." portion, the new directory still on "source_dir" instead of "dest_dir". You may disregard for the "tar" part, this is just a test script, just for me to understand 'sed' using the Positional Parameters ($1 $2 $3..). Could you shed some light for me please? I really appreciate any help.

File contents (*.txt) in source folder (source_dir):

/home/test/scripts/sed/source_dir/file1/file1.txt
/home/test/scripts/sed/source_dir/file2/file2/file2.txt
/home/test/scripts/sed/source_dir/file3/file3/file3/file3.txt

Objective:
I want to compress all those *.txt files on a new directory "dest_dir" but with their own subdirectories (according to souce folder), like the output below:

/home/test/scripts/sed/dest_dir/file1/file1.txt
/home/test/scripts/sed/dest_dir/file2/file2/file2.txt
/home/test/scripts/sed/dest_dir/file3/file3/file3/file3.txt

My script:

#!/bin/sh
input_file_type="txt"

find $1 -name *.$input_file_type | sort | while read in_file
do
        echo ">Input "$in_file

        out_file=$(echo $in_file | sed 's/$1/$2/g')

        echo ">Output "$out_file

        tar -zcvf "$in_file" "$out_file"
done

My Output with the script:

[test@centoslab sed]$ ./zip_test ~/scripts/sed/source_dir/ ~/scripts/sed/dest_dir/
>Input /home/test/scripts/sed/source_dir/file1/file1.txt
>Output /home/test/scripts/sed/source_dir/file1/file1.txt  <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
tar: Removing leading `/' from member names
/home/test/scripts/sed/source_dir/file1/file1.txt
>Input /home/test/scripts/sed/source_dir/file2/file2/file2.txt
>Output /home/test/scripts/sed/source_dir/file2/file2/file2.txt  <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
tar: Removing leading `/' from member names
/home/test/scripts/sed/source_dir/file2/file2/file2.txt
>Input /home/test/scripts/sed/source_dir/file3/file3/file3/file3.txt
>Output /home/test/scripts/sed/source_dir/file3/file3/file3/file3.txt  <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
tar: Removing leading `/' from member names
/home/test/scripts/sed/source_dir/file3/file3/file3/file3.txt
[test@centoslab sed]$

$1 $2 are positional parameters of the shell.
Use "quotes" around them to substitute them by their values, but protect the result from further expansions.
Use 'ticks' to get a $1 $2 literally.
For your sed script you want the substitution.

out_file=$(echo "$in_file" | sed "s/$1/$2/g")

You can also write a concatenations of 'string' and "string with $var":

out_file=$(echo "$in_file" | sed 's/'"$1"'/'"$2"'/g')

A concatenation of four strings. The sed sees one string and no quotes. (Tip: use echo to show it on the screen!)
The $var in the echo command should be in "quotes" as well, to have the variable substitution but no further expansions.

Thanks MadeInGermany for giving a hand. Seems I made it worked! :slight_smile:

Below is change I made:

#!/bin/sh
input_file_type="txt"

find $1 -name *.$input_file_type | sort | while read in_file
do
        echo ">Input "$in_file

        out_file=$(echo $in_file | sed 's,'$1','$2',g')

        echo ">Output "$out_file

        tar -zcvf "$in_file" "$out_file"
done

And my output, the destination folder is now come out correctly at last!:

[test@centoslab sed]$ ./zip_test ~/scripts/sed/source_dir/ ~/scripts/sed/dest_dir/
>Input /home/test/scripts/sed/source_dir/file1/file1.txt
>Output /home/test/scripts/sed/dest_dir/file1/file1.txt
tar: Removing leading `/' from member names
tar: /home/test/scripts/sed/dest_dir/file1/file1.txt: Cannot stat: No such file or directory
------------ snipped ------------
in_file="/home/test/scripts/sed/source_dir/file1/file1.txt"
echo tar -zcvf $in_file ${in_file/source_dir/dest_dir}
tar -zcvf /home/test/scripts/sed/source_dir/file1/file1.txt /home/test/scripts/sed/dest_dir/file1/file1.txt