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

How to prepend to a string that comes out of a pipe

I have two strings saved in a bash variable delimited by :. I want to get extract the second string, prepend that with THIS_VAR= and append it to a file named saved.txt

For example if myVar="abc:pqr", THIS_VAR=pqr should be appended to saved.txt.

This is what I have so far,

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

myVar="abc:pqr"
echo $myVar | cut -d ':' -f 2  >> saved.txt

How do I prepend THIS_VAR=?

>Solution :

printf 'THIS_VAR=%q\n' "${myVar#*:}"

See Shell Parameter Expansion and run help printf.

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