I am completely new to sed script. I want to make a script so that it asks user to enter value to be inserted in file. I am not able to find the correct code

so the line I have in the text file looks like this

path =

I want to input path from user so the line looks like:

path = /dev

Through trial and error I only have:

read -p "Enter path name: " PT
echo "$( sed -i '/path/ s/.*/& $PT/' textfile )"

which gives

path = $PT

can you help me with correct code

  • What is "sed script"? Are you new to the command sed or are you new to scripting? If the latter please indicate what shell you are using. – Anthon Sep 18 '17 at 16:19

Variable expansion doesn't happen within single quote, use double quote instead.

+ since your variable will contains / as part of path. with GNU sed you should use different substitution delimiter.

read -p "Enter path name: " PT
sed -i "\#path# s#.*#& $PT#" infile
read -p "Enter path name: " PT
sed -i "/path/ s#.*#& $PT#" textfile

You need to use double quotes, not single quotes. Single quotes protects a string from the shell, but in this case you want the shell to expand the value of $PT.


echo "$( ... )"

is the same as just ... (the stuff in the $( ... )).

Not the answer you're looking for? Browse other questions tagged or ask your own question.