LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This 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


Reply
  Search this Thread
Old 05-03-2024, 02:20 AM   #16
lvm_
Member
 
Registered: Jul 2020
Posts: 945

Rep: Reputation: 338Reputation: 338Reputation: 338Reputation: 338

Quote:
Originally Posted by mfoley View Post
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.
 
Old 05-03-2024, 01:28 PM   #17
wpeckham
LQ Guru
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS, Manjaro
Posts: 5,679

Rep: Reputation: 2712Reputation: 2712Reputation: 2712Reputation: 2712Reputation: 2712Reputation: 2712Reputation: 2712Reputation: 2712Reputation: 2712Reputation: 2712Reputation: 2712
BASH has some lovely string editing features that might be leveraged to convert or eliminate the special characters that are causing your headaches.

If that might help.
 
Old 05-06-2024, 05:30 PM   #18
mfoley
Senior Member
 
Registered: Oct 2008
Location: Columbus, Ohio USA
Distribution: Slackware
Posts: 2,576

Original Poster
Rep: Reputation: 179Reputation: 179
Quote:
Originally Posted by MadeInGermany View Post
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.
 
  


Reply

Tags
bash, escape



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
echoing a variable with a variable in its name? Speedy2k Linux - Newbie 5 12-31-2011 01:01 PM
[SOLVED] can't use ($) dollar sign in string ... php/fi 2.0.1 Linux Chips Programming 5 07-18-2011 09:37 AM
zsh PS1, dollar sign in the beginning, why? Mr. Alex Linux - Newbie 1 01-25-2011 09:48 AM
[SOLVED] Dollar sign in zsh Mr. Alex Linux - Newbie 3 01-24-2011 10:48 AM
Making grep find files containing '$' (the dollar sign char). stf92 Linux - Newbie 9 12-08-2010 11:16 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 11:05 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration