LinuxQuestions.org
Help answer threads with 0 replies.
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 02-02-2017, 09:11 PM   #1
FreddyMro
LQ Newbie
 
Registered: Jan 2017
Posts: 10

Rep: Reputation: Disabled
Question (Bash) Space-character problems?


Hi,

this is my code for /bin/bash:

function removeFirstSubstring {
tempVar=${1/$2/""}
echo $tempVar
}

var="one -two three -four five"
var=$(removeFirstSubstring "$var" "-two * ")
echo $var


I want to have the output "one -four five". At the moment it only gives me "one five" though.

Can you help me?

Thank you in advance!
 
Old 02-03-2017, 10:56 AM   #2
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,623

Rep: Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695Reputation: 2695
Quote:
Originally Posted by FreddyMro View Post
Hi,

this is my code for /bin/bash:

function removeFirstSubstring {
tempVar=${1/$2/""}
echo $tempVar
}

var="one -two three -four five"
var=$(removeFirstSubstring "$var" "-two * ")
echo $var


I want to have the output "one -four five". At the moment it only gives me "one five" though.

Can you help me?

Thank you in advance!
Your "-two *" is expanding to -two followed by a list of everything in the current folder. The results in a user function or bash internal function will depend upon what is in the folder, which is not optimal in this case. If you replace
Code:
"-two *"
with
Code:
"-two three"
you may get the desired output.

You do not say, but I infer that you are attempting to get a match to two strings, one fixed and one regex, in the same parameter. Can you confirm or contradict this inference?

Getting something CLOSE to what I did infer would be using single quotes instead of double quotes to delimit that parameter. In this case you would NOT get what you want, since the * would match ALL the remaining string rather than just the next word. Think about that a bit, and you may see a few possible solutions. (None of which will matter if my inference is false.)

I look forward to seeing your reply.
 
Old 02-03-2017, 11:27 AM   #3
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,897

Rep: Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019
Quote:
Originally Posted by wpeckham View Post
Your "-two *" is expanding to -two followed by a list of everything in the current folder.
Actually it's not because it's in double-quotes on the function call, and when it gets used within the ${1/$2/} expansion it is treated as a pattern rather than filename expansion.

The problem is simply that the '*' character matches zero or more of any character (including space) which is why it doesn't give him the result he wants.

If what he actually wants is to match to the end of the word then he'll need to enable the extglob shell option (shopt -s extglob) and use +([^ ]) instead of '*' as the pattern.

Code:
test@ws1:~$ function removeFirstSubstring {
> tempVar=${1/$2/""}
> echo $tempVar
> }
test@ws1:~$ shopt -s extglob
test@ws1:~$ removeFirstSubstring "one -two three -four five" "-two +([^ ])"
one -four five
test@ws1:~$

Last edited by GazL; 02-03-2017 at 11:28 AM.
 
2 members found this post helpful.
  


Reply

Tags
bash



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
[SOLVED] Bash: Problem with Escaping the Space Character ghborrmann Linux - Software 5 07-07-2016 05:58 PM
Writing a file character by character with a bash builtin command (script). stf92 Linux - Newbie 4 06-30-2012 08:41 PM
[SOLVED] How to escape bash-special character in a bash string? fantasy1215 Programming 12 03-10-2012 09:45 AM
[SOLVED] Bash regex to remove files with 3 character followed by a space Kearneyman Linux - General 6 09-15-2011 02:28 PM
Bash scripting: parsing a text file character-by-character Completely Clueless Programming 13 08-12-2009 09:07 AM

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

All times are GMT -5. The time now is 11:09 PM.

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