[SOLVED] how to escape $ (dollar sign) when echoing environment variable
Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
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.
Single quotes as lvm_ suggested can't be used because the $REPLY would not be substituted with its value.
Not here, earlier - in assignment. variable='file$name', not '$variable'. Have you ever wondered why ls puts single quotes around tricky file names? That's the reason.
For reading file names use IFS= and -r
Use the built-in string operators (parameter modifiers).
Code:
while IFS= read -r fn
do
x=${fn/\/mnt/} # deletes(substitutes) the first occurrence of /mnt (like sed 's#/mnt##')
# x=${fn/#\/mnt/} # deletes(substitutes) a /mnt at the beginning (like sed 's#^/mnt##')
# x=${fn#/mnt} # deletes a /mnt at the beginning (like sed 's#^/mnt##')
if [ -e "$x" ]; then echo yup; else echo nope; fi
done < fileList
If you must use sed then this is almost safe:
Code:
x=$(echo "$fn" | sed 's#/mnt##')
Exception: $fn is a echo option like -n or -e
100% safe is printf with a format:
Code:
x=$(printf "%s\n" "$fn" | sed 's#/mnt##')
$-expressions within " " (like "$fn") are evaluated/substituted; no further substitution or expansion happens.
An assignment (like x=val) does not need quotes (x="val"). But a command does:
Code:
printf "%s\n" "$(printf "%s\n" "$fn" | sed 's#/mnt##')"
$( ) is a sub shell; the quotes in it don't interfere with the main shell.
For demonstration, you could write it as
Code:
printf "%s\n" "$(
# sub shell starts
printf "%s\n" "$fn" | sed 's#/mnt##'
# sub shell ends
)"
Thanks MadeInGermany! All those suggested ideas worked for my example. I appreciate you addressing the actual question and giving a solution!! I will investigate that "while IFS= read -r fn" construct further to see how that works.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.