ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Originally posted by chii-chan I made this script to replace text using 'sed':
Code:
#!/bin/bash
txt_ori="original text"
txt_rplc="replacement text"
for text in "$@"
do
sed 's/$txt_ori/$txt_rplc/g' $text > text.out
done
But 'sed' doesn't seem to accept the variable. Anyway to make 'sed' to read the variables?
You are passing two arguments to sed. The first is the literal string s/$txt_ori/$txt_rplc/g, the second is the value of $text
I assume that the value it's not accepting is $txt_ori or $txt_rplc (you can find out exactly what you are passing by using echo instead of sed).
BASH treats anything in single-quotes as a literal string, and will not try to interpolate any variables. You probably want to be using double-quotes instead.
Originally posted by chii-chan I made this script to replace text using 'sed':
Code:
#!/bin/bash
txt_ori="original text"
txt_rplc="replacement text"
for text in "$@"
do
sed 's/$txt_ori/$txt_rplc/g' $text > text.out
done
But 'sed' doesn't seem to accept the variable. Anyway to make 'sed' to read the variables?
All you need to do (apart from learning how to treat metacharacters in the shell ) is use double quotes instead of the single ones, so the $ variables are expanded:
This may or may not work, depending on the contents of the values of the variables you are quoting.
If any of them contains a space, this will create a new parameter, which probably isn't what you want. If they contain <chevrons>, then you will start a file redirect. If they contain a pipe (|) then you will start a file redirect.
If you run
Code:
man bash
then you can read all about how and when BASH copes with special characters. For example:
Quote:
eval [arg ...]
The args are read and concatenated together into a single command. This command is then read and exe_cuted by the shell, and its exit status is returned as the value of eval. If there are no args, or only null arguments, eval returns 0.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.