I have files in which I am storing some credentials, I want shell script to read that file and check if current cred matches searched string, replace that at that location only.

I tried below code but it always append and does not even replace string.



  • FILESOURCE = my .txt file to read
  • TEXTTOSEARCH = string to search
  • TEXTTOREPLACE = text to replace

Note: I don't want to mess up existing file, instead it just open file for reading check if searched string found, then replace it, if not found dont do anything to file.

  • Does this answer your question? How can I make iconv replace the input file with the converted output? – roaima Sep 10 '20 at 7:04
  • sed will do a find and replace, that is find, then replace, if nothing is found, no replacement will occur. – Archemar Sep 10 '20 at 7:15
  • Are you sure your credentials (in particular $TEXTTOSEARCH) cannot contain the / or any characters that have a special meaning in the context of regular expressions? – AdminBee Sep 10 '20 at 8:02
  • Yes it contains special characters including / \ * ( ) _ - { } . @ ~ – Iván Carvallo Sep 10 '20 at 9:29

Of course it appends, since you are using >>, which means append.

What do you have in $TEXTTOSEARCH? Are there letters only or any special characters? If you have special characters that have special meaning in regular expression, you have to escape them in order to work correctly. For instance . means any character. You should replace it with \. in your $TEXTTOSEARCH. Look here for more details.

There are many ways how to handle your files. I would suggest you use


In that way sed will replace text in your file and make a backup with .bak extension. If you want it another way specify more in detail. Your question is not very clear, as you say you do not want to mess up existing file, and you want to replace text in that file?

  • Yes those have any special characters example G]07N!qRsf2@q3I?rxI8 BR4DzujZlTHS@-oA}9eq lf!_lOf5hN1!:b_v%zHJ 8D9QDG$VZ?@h.J,20p6K @FG7Vf9YR2{9HWfI5yo{ IKpgAqYC:O@4=r!$aI{m +;n*T.Plq5lXj3R:&_5L – Iván Carvallo Sep 10 '20 at 8:38
  • But its very complicated to use escape characters it can be any , purpose is to simplify task, we just give source string and file will be replaced with given string at perticular location. – Iván Carvallo Sep 10 '20 at 8:40
  • Doing escaping character stuff is tedious here. Don't we have anything that just as a full string replace with given new string (having special characters in it ? ) – Iván Carvallo Sep 10 '20 at 9:30
  • If using sed, you will have to escape characters. unix.stackexchange.com/questions/32907/… unix.stackexchange.com/questions/486131/… – nobody Sep 10 '20 at 9:39
  • is there any other way, let say I don't want to use SED command or escaping things – Iván Carvallo Sep 10 '20 at 10:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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